0

I have a map which I am trying to add a layer to, which the layer server supports the XYZoomLevel standard, creating a problem.

In order to begin the process of converting a quadkey to an XYZ, I need to get the current quadkey(s) which would be used to render the map outputted to a string and changed every time the key changes. How do I get the quad key as a string value?

Kevin
  • 383
  • 2
  • 11

1 Answers1

2

Here is sample code that allows you to add a layer, see

http://msdn.microsoft.com/en-us/library/hh846495.aspx:

MapTileLayer tileLayer = new MapTileLayer();
tileLayer.TileSource = "http://www.microsoft.com/maps/isdk/ajax/layers/lidar/{quadkey}.png";
map.TileLayers.Add(tileLayer);
map.SetView(new Location(48.03, -122.42), 11, MapAnimationDuration.None);

If you want to use a different uri scheme, you can use an alternative way by using the GetTileUri() method of the MapTileLayer class, that you will set with your own code to compose the uri. You can then convert the quadkey to xyz or the contrary.

Here a sample code where you use the method:

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            Bing.Maps.MapTileLayer layer = new Bing.Maps.MapTileLayer();
            layer.GetTileUri += layer_GetTileUri;
            this.map.TileLayers.Add(layer);
        }

        private async void layer_GetTileUri(object sender, Bing.Maps.GetTileUriEventArgs e)
        {
            e.Uri = this.ComposeMyCustomUri(e);
        }

Here, e is a specific parameter object of type GetTileUriEventArgs, see:

http://msdn.microsoft.com/en-us/library/jj672952.aspx

If you want to go from XYZ to quadkey, you can do it using this C# code:

    /// <summary>
    /// Converts tile XY coordinates into a QuadKey at a specified level of detail.
    /// </summary>
    /// <param name="tileX">Tile X coordinate.</param>
    /// <param name="tileY">Tile Y coordinate.</param>
    /// <param name="levelOfDetail">Level of detail, from 1 (lowest detail)
    /// to 23 (highest detail).</param>
    /// <returns>A string containing the QuadKey.</returns>
    public static string TileXYToQuadKey(int tileX, int tileY, int levelOfDetail)
    {
        StringBuilder quadKey = new StringBuilder();
        for (int i = levelOfDetail; i > 0; i--)
        {
            char digit = '0';
            int mask = 1 << (i - 1);
            if ((tileX & mask) != 0)
            {
                digit++;
            }
            if ((tileY & mask) != 0)
            {
                digit++;
                digit++;
            }
            quadKey.Append(digit);
        }
        return quadKey.ToString();
    }

Here is a more complete link from which the code is extracted, MSDN: http://msdn.microsoft.com/en-us/library/bb259689.aspx

Nicolas Boonaert
  • 2,954
  • 1
  • 20
  • 28