From e74c1c9bcf91208836bd7e409fb912efd2c08595 Mon Sep 17 00:00:00 2001 From: Alexander Krutov Date: Mon, 5 Jul 2021 17:42:01 +0300 Subject: [PATCH] Added Bing Maps --- DemoApp/FormMain.cs | 3 + MapControl/MapControl.csproj | 4 +- MapControl/TileServers/BingMapsTileServer.cs | 103 +++++++++++++++++++ 3 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 MapControl/TileServers/BingMapsTileServer.cs diff --git a/DemoApp/FormMain.cs b/DemoApp/FormMain.cs index b9a7809..c98dbff 100644 --- a/DemoApp/FormMain.cs +++ b/DemoApp/FormMain.cs @@ -90,6 +90,9 @@ public FormMain() new StamenTerrainTileServer(), new OpenTopoMapServer(), new OfflineTileServer(), + new BingMapsAerialTileServer(), + new BingMapsRoadsTileServer(), + new BingMapsHybridTileServer(), }; cmbTileServers.Items.AddRange(tileServers); diff --git a/MapControl/MapControl.csproj b/MapControl/MapControl.csproj index 016c861..a53f92f 100644 --- a/MapControl/MapControl.csproj +++ b/MapControl/MapControl.csproj @@ -1,10 +1,10 @@  net451 - 1.0.2 + 1.1.0 System.Windows.Forms.MapControl Map control for WindowsForms - © Alexander Krutov 2020 + © Alexander Krutov 2020-2021 true System.Windows.Forms.MapControl Map control for WindowsForms diff --git a/MapControl/TileServers/BingMapsTileServer.cs b/MapControl/TileServers/BingMapsTileServer.cs new file mode 100644 index 0000000..529a6ce --- /dev/null +++ b/MapControl/TileServers/BingMapsTileServer.cs @@ -0,0 +1,103 @@ +using System.Text; + +namespace System.Windows.Forms +{ + /// + /// Base class for Bing Maps tile providers + /// + public abstract class BingMapsTileServer : WebTileServer + { + /// + /// Not used here + /// + public override string UserAgent { get; set; } + + /// + /// Gets tile layer type: a = aerial, r = roads, h = hybrid + /// + protected abstract string Layer { get; } + + /// + public override string AttributionText => "© Bing Maps"; + + /// + public override int MinZoomLevel => 1; + + /// + /// Used to access random tile subdomains. + /// + private readonly Random _Random = new Random(); + + /// + 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"); + } + + /// + /// Converts x,y,z to QuadKey string used by MS maps. + /// + /// X-index of the tile. + /// Y-index of the tile. + /// Zoom level. + /// QuadKey string + /// See details: + 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(); + } + } + + /// + /// Represents Bing Maps Aerial web tile server. + /// + public class BingMapsAerialTileServer : BingMapsTileServer + { + /// + public override string Name => "Bing Maps (Aerial)"; + + /// + protected override string Layer => "a"; + } + + /// + /// Represents Bing Maps Roads web tile server. + /// + public class BingMapsRoadsTileServer : BingMapsTileServer + { + /// + public override string Name => "Bing Maps (Roads)"; + + /// + protected override string Layer => "r"; + } + + + /// + /// Represents Bing Maps Hybrid web tile server. + /// + public class BingMapsHybridTileServer : BingMapsTileServer + { + /// + public override string Name => "Bing Maps (Hybrid)"; + + /// + protected override string Layer => "h"; + } +}