windows8 Metro环境下Bing Map SDK的安装与使用

来源:互联网 发布:淘宝优惠券入口 编辑:程序博客网 时间:2024/05/25 08:14

必应地图现在有了针对Windows Store App的SDK,可以很方便的把Bing地图功能整合到自己的应用当中。 

最近参照官方说明文档操作了一下,果然比较好用,记录下经过以备日后使用。 

1.环境安装 

操作系统:   Windows8 Pro 64bit (RTM) 

开发工具:   Microsoft Visual Studio Professional 2012(RTM) 

SDK:           Bing Maps SDK for Windows Store apps 

注:如果操作系统是Windows 8 Release Preview,需要安装对应的预览版用SDK Bing Maps SDK for Windows Store apps 

2.创建Bing Map Account,并创建Bing Map Key  

登录https://www.bingmapsportal.com,如果已经有了一个Windows Live ID,点击sign用Windows Live ID登录并创建Bing Map Account。 

点击页面左侧[My Account]里的[Create or view keys],输入信息后点击Submit以创建Bing Map Key,创建完成后在画面下方可以查看到生成的Bing Map Key  

是一个64位英数字符号混合的字串。 


3.下载官方实例 Bing Maps C# LocationTracking Sample  

  • 程序运行前,需要先确认References中Bing Maps Application for Windows Store的参照正确无误。  
  • 在代码中全局搜索INSERT_YOUR_BING_MAPS_KEY字串,使用生成的Bing Map Key替换之。  
  • 在Solution Explorer窗口中右击Solution,从弹出菜单中选择Properties,将Configuration Properties中的Platform由ARM改为x64。 
  • 编译运行。 

4.代码相关 

要使用BingMap功能,最主要的就是Map控件,在XAML文件中定义入下: 

<bm:Map x:Name="map"  Credentials="INSERT_YOUR_BING_MAPS_KEY" Width="800" Height="450"/>

好了,这样应用中就有了最基本的Bing地图功能了,超简单吧。 

下面是调整Map属性的代码: 

        map.MapType = MapType.Road;     //道路模式        map.MapType = MapType.Aerial;    //航空摄影模式        map.MapType = MapType.Birdseye;  //鸟瞰模式        map.Heading += 90;               //逆时针旋转90度        map.Heading -= 90;               //顺时针旋转90度        TimeSpan animationDuration = new TimeSpan(0, 0, 0, 0, 500);  //设定延迟加载时间        map.SetHeading(map.Heading - 90, animationDuration);         //旋转90度后延迟加载地图


接下来结合定位功能:  

        LocationIcon locationIcon;        Geoposition currentPosition = await geolocator.GetGeopositionAsync();        Location location = new Location(currentPosition .Coordinate.Latitude, currentPosition .Coordinate.Longitude);        map.SetView(location, 10.0f);

现在地图的中点就是当前的位置了。  

在地图上添加图钉,XAML上直接定义: 

        <bm:Map ZoomLevel="10" Credentials="INSERT_YOUR_BING_MAPS_KEY" >            <bm:Map.Center>                <bm:Location Latitude="47.610039" Longitude="-122.342207" />            </bm:Map.Center>            <bm:Map.Children>                <bm:Pushpin>                    <bm:MapLayer.Position>                        <bm:Location Latitude="47.610039" Longitude="-122.342207" />                    </bm:MapLayer.Position>                </bm:Pushpin>            </bm:Map.Children>        </bm:Map>

在代码中为制定坐标添加图钉: 

        MapLayer mPinLayer = new MapLayer();        map.Children.Add(mPinLayer);        LocationCollection mPolyShapeLocations = new LocationCollection();        mPolyShapeLocations.Add(new Location(15, -15));        mPolyShapeLocations.Add(new Location(30, -90));        mPolyShapeLocations.Add(new Location(-45, -15));        mPolyShapeLocations.Add(new Location(30, 90));        mPolyShapeLocations.Add(new Location(60, 60));        for (int i = 0; i < mPolyShapeLocations.Count; i++)        {                Pushpin pin = new Pushpin();                pin.Text = i.ToString();                mPinLayer.Children.Add(pin);                MapLayer.SetPosition(pin, mPolyShapeLocations[i]);        }

结合上面的代码,设定图钉后,再在各个指定坐标间连线: 

        MapLayer mPinLayer = new MapLayer();        map.Children.Add(mPinLayer);        MapShapeLayer mShapeLayer = new MapShapeLayer();        map.ShapeLayers.Add(mShapeLayer);        LocationCollection mPolyShapeLocations = new LocationCollection();        mPolyShapeLocations.Add(new Location(15, -15));        mPolyShapeLocations.Add(new Location(30, -90));        mPolyShapeLocations.Add(new Location(-45, -15));        mPolyShapeLocations.Add(new Location(30, 90));        mPolyShapeLocations.Add(new Location(60, 60));        for (int i = 0; i < mPolyShapeLocations.Count; i++)        {                Pushpin pin = new Pushpin();                pin.Text = i.ToString();                mPinLayer.Children.Add(pin);                MapLayer.SetPosition(pin, mPolyShapeLocations[i]);        }        MapPolyline polyline = new MapPolyline();        polyline.Color = Windows.UI.Colors.Red;        polyline.Locations = mPolyShapeLocations;        mShapeLayer.Shapes.Add(mapShape);


至此,已经对Bing Map SDK 的安装和使用做了简单的说明,对于Metro App中内嵌Bing Map功能的需求,应该可以满足大部分的使用需要了。