|
| 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