下午开始看有关地图加载的视频,因Map控件默认加载的是Bing地图,所以是英文的,经过一翻网上查阅后,晚上在Map控件上加载了Google的地图,中文显示,甚是高兴,但却不知加载Google地图地址(“http://mt{0}.google.com/vt/lyrs=m@107&hl=en&x={1}&y={2}&z={3}&s=Ga”)和重载函数(“public override Uri GetUri(int x, int y, int zoomLevel)”)的具体意思,有待研究,在此也请教看到此文章的大侠们能给予帮助,多谢指导O(∩_∩)O~
基本代码示例如下:
先建立一个加载谷歌地图的类MyGoogleTileSource继承与TileSource,此代码来自网络他人所写,不是很明白,希望大侠能给以指导
public class MyGoogleTileSource:TileSource
    {
        public MyGoogleTileSource()
            : base("http://mt{0}.google.com/vt/lyrs=m@107&hl=en&x={1}&y={2}&z={3}&s=Ga")
        { }
        public override Uri GetUri(int x, int y, int zoomLevel)
        {
            //return base.GetUri(x, y, zoomLevel);
            int num = ((x & 1) << 1) | (y & 1);
            return new Uri(string.Format(this.UriFormat, num, x, y, zoomLevel));
        }
    }
在页面添加命名空间
xmlns:local="clr-namespace:MyBingMap"(此处的MyBingMap是我建立此项目的命名空间名,改为自己的即可)
xmlns:my="clr-namespace:Microsoft.Phone.Controls.Maps;assembly=Microsoft.Phone.Controls.Maps"
xmlns:mycore="clr-namespace:Microsoft.Phone.Controls.Maps.Core;assembly=Microsoft.Phone.Controls.Maps"(引用此空间的具体作用还希望看到的大侠给解说一下,谢谢)
 
添加一Map控件BingMap,在此Map控件中调用此类,加载Google地图,默认地图中心坐标是济南附近
<my:Map ZoomLevel="10" Center="36.66, 117" CredentialsProvider="As_UXqse9hiGMZysmrujLoin9vEzS52IQ7xY7GF9J5_xA-w-0NICrk6h3JsjlbTi" Height="494" HorizontalAlignment="Left" Margin="6,6,0,0" Name="BingMap" VerticalAlignment="Top" Width="444" >
  <my:Map.Mode>
    <mycore:MercatorMode></mycore:MercatorMode>
  </my:Map.Mode>
  <my:Map.Children>
    <my:MapTileLayer Height="492">
      <my:MapTileLayer.TileSources>
        <local:MyGoogleTileSource/>
      </my:MapTileLayer.TileSources>
    </my:MapTileLayer>
  </my:Map.Children>
</my:Map>
添加两个TextBox控件txtLatitude和txtLongitude,用于填写纬度和经度,添加一个Button控件btnCenter用于定位,实现Button控件btnCenter的Click事件,具体代码如下:
private void btnCenter_Click(object sender, RoutedEventArgs e)
        {
            if (txtLatitude.Text != "" || txtLongitude.Text != "")
            {
                double latitude = 0;
                double longitude = 0;
                double.TryParse(txtLatitude.Text, out latitude);
                double.TryParse(txtLongitude.Text, out longitude);
      BingMap.SetView(new GeoCoordinate(latitude, longitude), 10);
            }
            else
            {
                BingMap.SetView(new GeoCoordinate(36.66, 117), 10);
            }
        }
再添加两个实现缩放地图大小的Button控件btnElarge和btnLessen用于调节地图级别,实现各自的Click事件,代码如下:
private void btnElarge_Click(object sender, RoutedEventArgs e)
        {
            BingMap.ZoomLevel++;
        }

private void btnLessen_Click(object sender, RoutedEventArgs e)
        {
            BingMap.ZoomLevel--;
        }
由于本人也是初学基于Silverlight的WP7程序开发,还希望大侠们能多给予帮助,谢谢O(∩_∩)O~
效果如图所示:
 

作者: 任意飞 发表于 2011-07-05 14:36 原文链接

推荐.NET配套的通用数据层ORM框架:CYQ.Data 通用数据层框架
新浪微博粉丝精灵,刷粉丝、刷评论、刷转发、企业商家微博营销必备工具"