问题描述
我试图用一个自定义的地形设置为Bing地图。我所试图做的是类似于the这里的问题,但我想了解一个URI格式,因此我可以承载的应用程序内的瓦块。究其原因,我想在本地举办,这是因为我想尽可能多的限制网络流量从我的应用程序成为可能。
I am trying to use a custom tileset for Bing maps. What I am trying to do is similar to the question here, but I'm trying to understand how a URI is formatted so that I can host the tiles within the application. The reason I am trying to host this locally is because I want to limit network traffic out from my application as much as possible.
有没有在本地主机的地图图块任何教程或更深入的教程就如何有URI点至本地存储路径?
Are there any tutorials on hosting the map tiles locally or a more in depth tutorial on how to have the URI point to a local storage path?
在此先感谢您的任何建议。
Thanks in advance for any advice.
推荐答案
在平铺图计划的Bing地图(或谷歌地图或OpenStreetMap的)中央的一点是,每个地图图块是由三个参数确定。这些缩放级别(一种典型地从0或1至约20)和内缩放级别瓦片的x和y的索引。在给定的缩放级别的Z x和y指数范围从0到2 ^ Z-1。在缩放级别0有一个片,在第1级有2×2的瓦,在第2级有4×4瓦片等
The central point in the Bing Maps (or Google Maps or OpenStreetMap) tiled map scheme is that each map tile is identified by three parameters. These are the zoom level (that typically ranges from 0 or 1 to about 20) and the x and y index of a tile within a zoom level. In a given zoom level z the x and y index range from 0 to 2^z-1. In zoom level 0 there is one tile, in level 1 there are 2x2 tile, in level 2 there are 4x4 tiles and so on.
大多数地图图块提供商像OpenStreetMap的或谷歌地图直接反映在他们的瓦片的URI这三个参数。 OpenStreetMap的,例如提供了地图图块的URI http://tile.openstreetmap.org/z/ X / y.png 。
Most map tile providers like OpenStreetMap or Google Maps directly reflect these three parameters in their tile URIs. OpenStreetMap for example provides map tiles by the URI http://tile.openstreetmap.org/z/x/y.png.
在派生 TileSource 类,你重写的getURI方法,以提供一个URI为三瓦参数。对于OpenStreetMap的一致好评瓷砖这样的衍生TileSource可能是这样的:
In a derived TileSource class you override the GetUri method to provide an URI for the three tile parameters. For OpenStreetMap alike tiles such a derived TileSource might look like this:
public class MyTileSource : Microsoft.Maps.MapControl.WPF.TileSource
{
public override Uri GetUri(int x, int y, int zoomLevel)
{
return new Uri(UriFormat.
Replace("{x}", x.ToString()).
Replace("{y}", y.ToString()).
Replace("{z}", zoomLevel.ToString()));
}
}
对于一些愚蠢的技术细节,在必应地图WPF控件TileLayer类,你也必须派生自己TileLayer类,以使在使用XAML:
For some stupid technical detail in the Bing Maps WPF Control TileLayer class you would also have to derive your own TileLayer class to enable usage in XAML:
public class MyTileLayer : Microsoft.Maps.MapControl.WPF.MapTileLayer
{
public MyTileLayer()
{
TileSource = new MyTileSource();
}
public string UriFormat
{
get { return TileSource.UriFormat; }
set { TileSource.UriFormat = value; }
}
}
您会然后用它在地图控件如下图所示,其中的XAML命名空间 M
引用 Microsoft.Maps.MapControl.WPF
和本地
引用包含派生TileLayer的命名空间。
You would then use it in the Map Control like below where the XAML namespace m
references Microsoft.Maps.MapControl.WPF
and local
references the namespace that contains the derived TileLayer.
<m:Map>
<m:Map.Mode>
<!-- set empty map mode, i.e. remove default map layer -->
<m:MercatorMode/>
</m:Map.Mode>
<local:MyTileLayer UriFormat="http://tile.openstreetmap.org/{z}/{x}/{y}.png"/>
</m:Map>
而不是创建一个HTTP URI,你可能现在还可以创建一个URI本地文件。你可以例如组织地图瓦片中的目录结构的目录为缩放等级,用于x索引和用于y索引文件名称的子目录。您可以设置 UriFormat
属性的方式,指定一个本地路径:
Instead of creating a http URI you might now also create an URI for a local file. You could for example organize the map tiles in a directory structure with a directory for the zoom level, a subdirectory for the x index and a file name for the y index. You could set the UriFormat
property in a way that specifies a local path:
<local:MyTileLayer UriFormat="file:///C:/Tiles/{z}/{x}/{y}.png"/>
该覆盖的getURI方法也可能直接创建适当的本地文件的URI,而无需使用 UriFormat
属性:
public override Uri GetUri(int x, int y, int zoomLevel)
{
string rootDir = ...
string path = Path.Combine(rootDir, zoomLevel.ToString(), x.ToString(), y.ToString());
return new Uri(path);
}
您可能需要阅读更多有关OpenStreetMap的如何处理地图图块名的。
You may want to read more about how OpenStreetMap handles map tile names.
这篇关于将自定义瓷砖在WPF Bing地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!