windows phone7中使用bing中文地图和Google地图

之前在windows phone中使用map控件时,一直加载出来的是英文的bing map,一直以为要等ms提供汉化的map;而同时google和baidu等未发现对应的map sdk或api,那个苦恼啊,使用英文bing map那个痛苦啊;期间看到一些使用加载titlesource 加载google地图或者bing map中文地图的方法;但是这些方法都是非正式处理方法,担心应用发布后会遇到问题;后来从nokia那得到消息,nokia会出正式的windows phone地图,但是需要走商业合同... ...,我只整理了下如何加载中文bingmap和google map的方法;源码

实现的大致思路都是重写titlesource,在bingmap control中添加一个层,将对应titlesource加载到这个层上显示;

方法是定义两个类继承自Microsoft.Phone.Controls.Maps.TileSource,然后重写geturl方法;

代码如下:

View Code
    public class GoogleTitleSource : TileSource
    {
        public GoogleTitleSource()
            : base("http://khm{0}.google.com/kh/v=47&x={1}&y={2}&z={3")
        {

        }

        public override Uri GetUri(int x, int y, int zoomLevel)
        {
            return new Uri(string.Format(this.UriFormat, x % 4, x, y, zoomLevel));
        }

    }

    public class BingChinaTitleSource : TileSource
    {
        public BingChinaTitleSource()
            : base("http://r2.tiles.ditu.live.com/tiles/r{quadkey}.png?g=4")
        {

        }
    }

再在page loaded事件中添加对应map的层和源

View Code
  private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            //bing
            MapTileLayer bingtileLayer = new MapTileLayer();
            bingtileLayer.Width = 431;
            bingtileLayer.Height = 540;

            GeoCoordinate center = new GeoCoordinate(36,104);
            TileSource tileSource = new BingChinaTitleSource();
            bingtileLayer.TileSources.Add(tileSource);
            bingtileLayer.Opacity = 0.9;
            bingMap.Children.Add(bingtileLayer);
            bingMap.Mode = new MercatorMode();
            bingMap.Center = center;

            //google
            MapTileLayer googletileLayer = new MapTileLayer();
            googletileLayer.Width = 431;
            googletileLayer.Height = 540;
       
            TileSource googletileSource = new GoogleTitleSource();
            googletileLayer.TileSources.Add(tileSource);
            googletileLayer.Opacity = 0.9;
            google.Children.Add(googletileLayer);
            google.Mode = new MercatorMode();
            google.Center = center;

            bingEng.Center = center;
        }

ui中使用全景视图,分别添加两个Map控件

View Code
 <controls:Panorama Grid.Row="1" Height="722" HorizontalAlignment="Left" Margin="0,0,0,0" Title="maps" VerticalAlignment="Top" Width="479" FontSize="26.667" Style="{StaticResource PanoramaStyle1}">
        <controls:PanoramaItem Header="Bing Map Chinese" Style="{StaticResource PanoramaItemStyle1}">
            <Grid Height="579" Width="434">
                <my:Map x:Name="bingMap" Height="578" LogoVisibility="Collapsed" CopyrightVisibility="Collapsed" HorizontalAlignment="Left" Margin="3,1,0,0"
                         VerticalAlignment="Top" Width="431" ZoomLevel="3" />
            </Grid>
        </controls:PanoramaItem>
        <controls:PanoramaItem Header="Google Map" Style="{StaticResource PanoramaItemStyle1}">
            <Grid Height="578" Width="426">
                <my:Map x:Name="google" Height="578" LogoVisibility="Collapsed" CopyrightVisibility="Collapsed" HorizontalAlignment="Left" Margin="3,1,0,0"
                    VerticalAlignment="Top" Width="431" ZoomLevel="3" />
            </Grid>
        </controls:PanoramaItem>
        <controls:PanoramaItem Header="bing Map englsh" Style="{StaticResource PanoramaItemStyle1}">
            <Grid Height="557" Width="426">
                <my:Map x:Name="bingEng" Height="549" ZoomLevel="3" LogoVisibility="Collapsed" CopyrightVisibility="Collapsed" HorizontalAlignment="Left" Margin="3,1,0,0" VerticalAlignment="Top" Width="416" />
            </Grid>
        </controls:PanoramaItem>
    </controls:Panorama>

运行效果

bingchgmapsbing eng

从体验来说bing中文和google似乎差别不大,但是bing英文 ui显示元素太少;

源码

posted @ 2012-04-12 15:47  J默  阅读(2160)  评论(2编辑  收藏  举报