Skip to content

Commit

Permalink
Added Bing Maps
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderKrutov committed Jul 5, 2021
1 parent cf953c8 commit e74c1c9
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 2 deletions.
3 changes: 3 additions & 0 deletions DemoApp/FormMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ public FormMain()
new StamenTerrainTileServer(),
new OpenTopoMapServer(),
new OfflineTileServer(),
new BingMapsAerialTileServer(),
new BingMapsRoadsTileServer(),
new BingMapsHybridTileServer(),
};

cmbTileServers.Items.AddRange(tileServers);
Expand Down
4 changes: 2 additions & 2 deletions MapControl/MapControl.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net451</TargetFrameworks>
<Version>1.0.2</Version>
<Version>1.1.0</Version>
<Product>System.Windows.Forms.MapControl</Product>
<Description>Map control for WindowsForms</Description>
<Copyright>© Alexander Krutov 2020</Copyright>
<Copyright>© Alexander Krutov 2020-2021</Copyright>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Title>System.Windows.Forms.MapControl</Title>
<Summary>Map control for WindowsForms</Summary>
Expand Down
103 changes: 103 additions & 0 deletions MapControl/TileServers/BingMapsTileServer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using System.Text;

namespace System.Windows.Forms
{
/// <summary>
/// Base class for Bing Maps tile providers
/// </summary>
public abstract class BingMapsTileServer : WebTileServer
{
/// <summary>
/// Not used here
/// </summary>
public override string UserAgent { get; set; }

/// <summary>
/// Gets tile layer type: a = aerial, r = roads, h = hybrid
/// </summary>
protected abstract string Layer { get; }

/// <inheritdoc />
public override string AttributionText => "© <a href='https://www.bing.com/maps/'>Bing Maps</a>";

/// <inheritdoc />
public override int MinZoomLevel => 1;

/// <summary>
/// Used to access random tile subdomains.
/// </summary>
private readonly Random _Random = new Random();

/// <inheritdoc />
public override Uri GetTileUri(int x, int y, int z)
{
return new Uri($"https://ecn.t{_Random.Next(8)}.tiles.virtualearth.net/tiles/{Layer}{TileXYToQuadKey(x, y, z)}.jpeg?g=587");
}

/// <summary>
/// Converts x,y,z to QuadKey string used by MS maps.
/// </summary>
/// <param name="x">X-index of the tile.</param>
/// <param name="y">Y-index of the tile.</param>
/// <param name="z">Zoom level.</param>
/// <returns>QuadKey string</returns>
/// <remarks>See details: <see href="https://docs.microsoft.com/en-us/bingmaps/articles/bing-maps-tile-system"/></remarks>
private string TileXYToQuadKey(int x, int y, int z)
{
var quadKey = new StringBuilder();
for (int i = z; i > 0; i--)
{
char digit = '0';
int mask = 1 << (i - 1);
if ((x & mask) != 0)
{
digit++;
}
if ((y & mask) != 0)
{
digit++;
digit++;
}
quadKey.Append(digit);
}
return quadKey.ToString();
}
}

/// <summary>
/// Represents Bing Maps Aerial web tile server.
/// </summary>
public class BingMapsAerialTileServer : BingMapsTileServer
{
/// <inheritdoc />
public override string Name => "Bing Maps (Aerial)";

/// <inheritdoc />
protected override string Layer => "a";
}

/// <summary>
/// Represents Bing Maps Roads web tile server.
/// </summary>
public class BingMapsRoadsTileServer : BingMapsTileServer
{
/// <inheritdoc />
public override string Name => "Bing Maps (Roads)";

/// <inheritdoc />
protected override string Layer => "r";
}


/// <summary>
/// Represents Bing Maps Hybrid web tile server.
/// </summary>
public class BingMapsHybridTileServer : BingMapsTileServer
{
/// <inheritdoc />
public override string Name => "Bing Maps (Hybrid)";

/// <inheritdoc />
protected override string Layer => "h";
}
}

0 comments on commit e74c1c9

Please sign in to comment.