本节是WP7学习札记的第十六篇了,接近学习的尾声,本节主要讲Bing Maps控件的一般使用,不讲如何注册Bing Maps 账户,申请Bing Maps 使用key,内容页不包含使用Bing Maps SOAP服务计算路径~相关内容请参考MSDN的文档,地址为:http://msdn.microsoft.com/zh-cn/library/ff941096(v=vs.92).aspx
内容主要为:地图的缩放、地图模式的设置、定位、Pushpin等,废话不多说,附上代码:
MainPage.xaml的xaml代码如下:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<phone:PhoneApplicationPage
x:Class="BingMaps.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True" xmlns:my="clr-namespace:Microsoft.Phone.Controls.Maps;assembly=Microsoft.Phone.Controls.Maps">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="演示程序" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="Bing Maps" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<my:Map Height="475" HorizontalAlignment="Left" Name="map1" VerticalAlignment="Top" Width="456" />
<Button Content="+" Height="72" HorizontalAlignment="Left" Margin="0,481,0,0" Name="btnZoomOut" VerticalAlignment="Top" Width="160" Click="btnZoomOut_Click" />
<Button Content="-" Height="72" HorizontalAlignment="Left" Margin="0,535,0,0" Name="btnZoomIn" VerticalAlignment="Top" Width="160" Click="btnZoomIn_Click" />
<Button Content="地图" Height="72" HorizontalAlignment="Left" Margin="149,481,0,0" Name="btnRoad" VerticalAlignment="Top" Width="160" Click="btnRoad_Click" />
<Button Content="卫星" Height="72" HorizontalAlignment="Left" Margin="149,535,0,0" Name="btnAerial" VerticalAlignment="Top" Width="160" Click="btnAerial_Click" />
<Button Content="定位" Height="72" HorizontalAlignment="Left" Margin="296,481,0,0" Name="btnCenter" VerticalAlignment="Top" Width="160" Click="btnCenter_Click" />
<Button Content="图钉" Height="72" HorizontalAlignment="Left" Margin="296,535,0,0" Name="btnPushpin" VerticalAlignment="Top" Width="160" Click="btnPushpin_Click" />
</Grid>
</Grid>
<!--Sample code showing usage of ApplicationBar-->
<!--<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/>
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/>
<shell:ApplicationBar.MenuItems>
<shell:ApplicationBarMenuItem Text="MenuItem 1"/>
<shell:ApplicationBarMenuItem Text="MenuItem 2"/>
</shell:ApplicationBar.MenuItems>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>-->
</phone:PhoneApplicationPage>
MainPage.xaml.cs代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
//
using Microsoft.Phone.Controls.Maps;
using System.Device.Location;
namespace BingMaps
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private void btnZoomOut_Click(object sender, RoutedEventArgs e)
{
double zoom;
zoom = map1.ZoomLevel;
map1.ZoomLevel = ++zoom;
}
private void btnZoomIn_Click(object sender, RoutedEventArgs e)
{
double zoom;
zoom = map1.ZoomLevel;
map1.ZoomLevel = --zoom;
}
private void btnRoad_Click(object sender, RoutedEventArgs e)
{
map1.Mode = new RoadMode();
}
private void btnAerial_Click(object sender, RoutedEventArgs e)
{
map1.Mode = new AerialMode();
}
private void btnCenter_Click(object sender, RoutedEventArgs e)
{
map1.SetView(new GeoCoordinate(38.90, -77.04), 15);
}
private void btnPushpin_Click(object sender, RoutedEventArgs e)
{
MapLayer mapLayer = new MapLayer();
map1.Children.Add(mapLayer);
Pushpin pushpin = new Pushpin() {
Location= new GeoCoordinate(38.8976,-77.0365),
Content="白宫"
};
mapLayer.AddChild (pushpin, pushpin.Location);
}
}
}
程序运行效果如下: