Skip to content

Commit e74c1c9

Browse files
Added Bing Maps
1 parent cf953c8 commit e74c1c9

3 files changed

Lines changed: 108 additions & 2 deletions

File tree

DemoApp/FormMain.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ public FormMain()
9090
new StamenTerrainTileServer(),
9191
new OpenTopoMapServer(),
9292
new OfflineTileServer(),
93+
new BingMapsAerialTileServer(),
94+
new BingMapsRoadsTileServer(),
95+
new BingMapsHybridTileServer(),
9396
};
9497

9598
cmbTileServers.Items.AddRange(tileServers);

MapControl/MapControl.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFrameworks>net451</TargetFrameworks>
4-
<Version>1.0.2</Version>
4+
<Version>1.1.0</Version>
55
<Product>System.Windows.Forms.MapControl</Product>
66
<Description>Map control for WindowsForms</Description>
7-
<Copyright>© Alexander Krutov 2020</Copyright>
7+
<Copyright>© Alexander Krutov 2020-2021</Copyright>
88
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
99
<Title>System.Windows.Forms.MapControl</Title>
1010
<Summary>Map control for WindowsForms</Summary>
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
using System.Text;
2+
3+
namespace System.Windows.Forms
4+
{
5+
/// <summary>
6+
/// Base class for Bing Maps tile providers
7+
/// </summary>
8+
public abstract class BingMapsTileServer : WebTileServer
9+
{
10+
/// <summary>
11+
/// Not used here
12+
/// </summary>
13+
public override string UserAgent { get; set; }
14+
15+
/// <summary>
16+
/// Gets tile layer type: a = aerial, r = roads, h = hybrid
17+
/// </summary>
18+
protected abstract string Layer { get; }
19+
20+
/// <inheritdoc />
21+
public override string AttributionText => "© <a href='https://www.bing.com/maps/'>Bing Maps</a>";
22+
23+
/// <inheritdoc />
24+
public override int MinZoomLevel => 1;
25+
26+
/// <summary>
27+
/// Used to access random tile subdomains.
28+
/// </summary>
29+
private readonly Random _Random = new Random();
30+
31+
/// <inheritdoc />
32+
public override Uri GetTileUri(int x, int y, int z)
33+
{
34+
return new Uri($"https://ecn.t{_Random.Next(8)}.tiles.virtualearth.net/tiles/{Layer}{TileXYToQuadKey(x, y, z)}.jpeg?g=587");
35+
}
36+
37+
/// <summary>
38+
/// Converts x,y,z to QuadKey string used by MS maps.
39+
/// </summary>
40+
/// <param name="x">X-index of the tile.</param>
41+
/// <param name="y">Y-index of the tile.</param>
42+
/// <param name="z">Zoom level.</param>
43+
/// <returns>QuadKey string</returns>
44+
/// <remarks>See details: <see href="https://docs.microsoft.com/en-us/bingmaps/articles/bing-maps-tile-system"/></remarks>
45+
private string TileXYToQuadKey(int x, int y, int z)
46+
{
47+
var quadKey = new StringBuilder();
48+
for (int i = z; i > 0; i--)
49+
{
50+
char digit = '0';
51+
int mask = 1 << (i - 1);
52+
if ((x & mask) != 0)
53+
{
54+
digit++;
55+
}
56+
if ((y & mask) != 0)
57+
{
58+
digit++;
59+
digit++;
60+
}
61+
quadKey.Append(digit);
62+
}
63+
return quadKey.ToString();
64+
}
65+
}
66+
67+
/// <summary>
68+
/// Represents Bing Maps Aerial web tile server.
69+
/// </summary>
70+
public class BingMapsAerialTileServer : BingMapsTileServer
71+
{
72+
/// <inheritdoc />
73+
public override string Name => "Bing Maps (Aerial)";
74+
75+
/// <inheritdoc />
76+
protected override string Layer => "a";
77+
}
78+
79+
/// <summary>
80+
/// Represents Bing Maps Roads web tile server.
81+
/// </summary>
82+
public class BingMapsRoadsTileServer : BingMapsTileServer
83+
{
84+
/// <inheritdoc />
85+
public override string Name => "Bing Maps (Roads)";
86+
87+
/// <inheritdoc />
88+
protected override string Layer => "r";
89+
}
90+
91+
92+
/// <summary>
93+
/// Represents Bing Maps Hybrid web tile server.
94+
/// </summary>
95+
public class BingMapsHybridTileServer : BingMapsTileServer
96+
{
97+
/// <inheritdoc />
98+
public override string Name => "Bing Maps (Hybrid)";
99+
100+
/// <inheritdoc />
101+
protected override string Layer => "h";
102+
}
103+
}

0 commit comments

Comments
 (0)