如何在 Windows Phone 8 中的地图中显示路线和方向
2012/11/14
适用于:仅限于 Windows Phone 8。
本主题介绍如何能够显示从手机的当前位置到 Windows Phone 8 地图中的指定位置的线路和方向。
获取手机的当前位置
-
在 Visual Studio 2012 Express for Windows Phone 中创建新的 Windows Phone 8 项目。
-
在 MainPage.xaml.cs 的 MainPage 类中添加以下代码以获取手机的当前位置。此代码将手机当前的位置坐标添加到 MyCoordinates。
public partial class MainPage : PhoneApplicationPage { List<GeoCoordinate> MyCoordinates = new List<GeoCoordinate>(); // Constructor public MainPage() { InitializeComponent(); this.GetCoordinates(); } private async void GetCoordinates() { //Getting Phone's current location Geolocator MyGeolocator = new Geolocator(); MyGeolocator.DesiredAccuracyInMeters = 5; Geoposition MyGeoPosition = null; try { MyGeoPosition = await MyGeolocator.GetGeopositionAsync(TimeSpan.FromMinutes(1), TimeSpan.FromSeconds(10)); } catch (UnauthorizedAccessException) { MessageBox.Show("Location is disabled in phone settings"); } catch (Exception ex) { // something else happened acquring the location // ex.HResult can be read to know the specific error code but it is not recommended } MyCoordinates.Add(new GeoCoordinate(MyGeoPosition.Coordinate.Latitude, MyGeoPosition.Coordinate.Longitude)); } }
显示从手机的当前位置到地图中某一位置的路线。
-
在 MainPage.xaml 中,在名为 LayoutRoot 的网格下添加一个 Map 控件,如以下代码所示。有关添加Map 控件的更多信息,请参见如何将地图控件添加到 Windows Phone 8 中的页面中。
<maps:Map x:Name="MyMap" Grid.Row="1" Center="47.603848, -122.029953" ZoomLevel="11"/>
-
在 MainPage.xaml.cs 的 MainPage 类中创建以下成员变量。此代码将用户输入的目标地址设置为SearchTerm 属性,然后将该地址转换为地理坐标。随后,地理坐标被设置为 GeocodeQuery.GeoCoordinate属性。
RouteQuery MyQuery = null; GeocodeQuery Mygeocodequery = null;
-
在 GetCoordinates() 方法中,添加以下代码。
Mygeocodequery = new GeocodeQuery(); Mygeocodequery.SearchTerm = "Seattle WA"; Mygeocodequery.GeoCoordinate = new GeoCoordinate(MyGeoPosition.Coordinate.Latitude, MyGeoPosition.Coordinate.Longitude);
-
将名为 Mygeocodequery_QueryCompleted 的事件处理程序添加到 QueryCompleted 事件,并调用QueryAsync 方法。此代码创建一个新的 RouteQuery 对象,并将当前指定目标的坐标存储在其 Waypoints 属性中。它还将名为 MyQuery_QueryCompleted 事件处理程序添加到 QueryCompleted 事件。
Mygeocodequery.QueryCompleted += Mygeocodequery_QueryCompleted; Mygeocodequery.QueryAsync();
-
在 Mygeocodequery_QueryCompleted 事件处理程序中,添加以下代码。
void Mygeocodequery_QueryCompleted(object sender, QueryCompletedEventArgs<IList<MapLocation>> e) { if (e.Error == null) { MyQuery = new RouteQuery(); MyCoordinates.Add(e.Result[0].GeoCoordinate); MyQuery.Waypoints = MyCoordinates; MyQuery.QueryCompleted += MyQuery_QueryCompleted; MyQuery.QueryAsync(); Mygeocodequery.Dispose(); } }
-
在 MyQuery_QueryCompleted 事件处理程序中,添加以下代码。它将在地图上 MyCoordinates 中的两个地理坐标之间添加线路。
void MyQuery_QueryCompleted(object sender, QueryCompletedEventArgs<Route> e) { if (e.Error == null) { Route MyRoute = e.Result; MapRoute MyMapRoute = new MapRoute(MyRoute); MyMap.AddRoute(MyMapRoute); MyQuery.Dispose(); } }
显示从手机的当前位置到地图中某一位置的方向。
-
在 MainPage.xaml 中,添加以下代码以在现有地图控件下创建 LongListSelector 控件。这将用于显示两个位置之间的方向。
<phone:PhoneApplicationPage.Resources> <DataTemplate x:Key="RouteListTemplate"> <TextBlock Text="{Binding}" FontSize="{StaticResource PhoneFontSizeMedium}" Margin="5,5,0,0"/> </DataTemplate> </phone:PhoneApplicationPage.Resources> <!--LayoutRoot is the root grid where all page content is placed--> <Grid x:Name="LayoutRoot" Background="Transparent"> <Grid.RowDefinitions> <RowDefinition Height="auto"/> <RowDefinition Height="*"/> <RowDefinition Height="auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <TextBlock Text="Map Route" Grid.Row="0" FontSize="{StaticResource PhoneFontSizeLarge}" Margin="0,0,0,20"/> <maps:Map x:Name="MyMap" Grid.Row="1" Center="47.603848, -122.029953" ZoomLevel="11"/> <TextBlock Text="Route Directions" Grid.Row="2" FontSize="{StaticResource PhoneFontSizeLarge}" Margin="0,10,0,20"/> <phone:LongListSelector x:Name="RouteLLS" Grid.Row="3" Background="Transparent" ItemTemplate="{StaticResource RouteListTemplate}" LayoutMode="List" IsGroupingEnabled="False"/> </Grid>
-
在 MainPage.cs 中,将以下代码添加到 MyQuery_QueryCompleted 事件处理程序以显示两个位置之间的线路。
void MyQuery_QueryCompleted(object sender, QueryCompletedEventArgs<Route> e) { if (e.Error == null) { Route MyRoute = e.Result; MapRoute MyMapRoute = new MapRoute(MyRoute); MyMap.AddRoute(MyMapRoute); List<string> RouteList = new List<string>(); foreach (RouteLeg leg in MyRoute.Legs) { foreach (RouteManeuver maneuver in leg.Maneuvers) { RouteList.Add(maneuver.InstructionText); } } RouteLLS.ItemsSource = RouteList; MyQuery.Dispose(); } }
以下屏幕快照显示了本主题中所用示例的结果。