2014-10-04 111 views
0

我有一張地圖,我試圖添加一個圖層,其中圖層服務器支持XYZoomLevel標準,從而產生問題。C#/ Bing Maps Win8.1 - 獲取當前quadkey作爲字符串

爲了開始將quadkey轉換爲XYZ的過程,我需要獲取當前的quadkey,這些quadkey將用於呈現輸出到字符串的映射,並在每次鍵更改時更改。如何將四鍵作爲字符串值獲取?

回答

2

下面是示例代碼,使您可以添加圖層,看到

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

如果要使用不同的URI方案,您可以通過使用GetTileUri()使用的另一種方式MapTileLayer類的方法,您將使用自己的代碼設置組合uri。然後,您可以將quadkey轉換爲xyz或相反。

在這兒,你使用的方法的示例代碼:

 /// <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); 
     } 

這裏,eGetTileUriEventArgs類型的特定參數對象,請參見:

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

如果你想從去XYZ到quadkey,你可以使用這個C#代碼:

/// <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(); 
    } 

這裏是一個更完整的鏈接,代碼是從中提取的,MSDN:http://msdn.microsoft.com/en-us/library/bb259689.aspx