如何在WindowsPhone Bing Map控件中显示必应中国中文地图、谷歌中国中文地图。

原文: 如何在WindowsPhone Bing Map控件中显示必应中国中文地图、谷歌中国中文地图。

最近正好有点业余时间,所以在做做各种地图。Bing Map控件本身就能显示必应地图,但是很遗憾微软在Bing Map控件中只提供两种地图模式:全球道路地图RoadMode、全球卫星地图AerialMode,卫星地图对咱们这些个凡人是没啥用的,全球道路地图又不显示天朝地图,因为咱们伟大的天朝政府要求在天朝内发布的地图都要加上随机偏移量才能发布。所以每个国际电子地图提供商(谷歌、微软、高德等等)都会有两类地图:全球地图和中国地图。微软BingMap控件内置没有提供中国地图,所以如果在BingMap控件里移动地图到天朝区域,除了能显示城市名之外,一片空白。。。怎么办呢?

解决方法前提:微软的BingMap控件显示地图的原理是类似网页div标签的层,我们在上面看到的地图其实就是一个层,你要在上面叠上几个层都可以。咱们只要把原先显示全球地图的那个层替换成显示中国地图的层就可以了。

显示必应中国地图代码如下:

//必应中文道路地图

    public class BingChinaRoadMode : MercatorMode

    {

     //这个类见名知意:就是层嘛。

        MapTileLayer layer = new MapTileLayer();



        public override UIElement Content

        {

            get

            {

                return layer;

            }

        }



        public BingChinaRoadMode()

        {

            layer.TileSources.Add(new BingChinaRoadTileSource());

        }



     //类中类

        //必应中文地图的TitleSource

        private class BingChinaRoadTileSource : TileSource

        {

            public BingChinaRoadTileSource()

                : base("http://r2.tiles.ditu.live.com/tiles/r{quadkey}.png?g=4")  //指定BingMap控件从网络上获取的中国地图图片来源url地址,注:此url可以从网页版必应中国地图的Http请求得到。

            {



            }

        }

    }

 

//指定BingMap控件的地图模式为咱们自定义的 BingChinaRoadMode

BingMap.Mode = new BingChinaRoadMode();

然后BingMap控件就显示必应中国地图啦。

 

 

二:

好吧,送佛送到西,显示谷歌地图的代码如下:

    //谷歌中文道路地图

    public class GoogleChinaRoadMode : MercatorMode

    {

        MapTileLayer layer = new MapTileLayer();



        public override UIElement Content

        {

            get

            {

                return layer;

            }

        }



        public GoogleChinaRoadMode()

        {

            layer.TileSources.Add(new GoogleChinaRoadTileSource());

        }



        //谷歌道路地图TitleSource

        private class GoogleChinaRoadTileSource : TileSource

        {

            public GoogleChinaRoadTileSource()

                : base("http://mt{0}.google.com/vt/lyrs=m@128&hl=zh&x={1}&y={2}&z={3}&s=")

            { }



            public override System.Uri GetUri(int x, int y, int zoomLevel)

            {

                return new System.Uri(string.Format(UriFormat, new System.Random().Next() % 4, x, y, zoomLevel));

            }

        }

    }

 

完了,那是不是也可以在BingMap控件中显示:百度地图,说对了,前途是光明的、道路是曲折的。下一篇讲如何显示百度地图,这个就不像显示必应中国地图和谷歌地图那么简单了。

你可能感兴趣的:(windows)