我已经开始在Windows手机应用程序中使用地图,我已经知道如何获得当前坐标,但我不确定如何将其绘制为地图上的图钉。
这就是我到目前为止调用GetCoordinates方法并在单击按钮时导航到地图的内容。有人知道我怎么把坐标传递给地图并把它画成图钉吗?
private async Task GetCoordinates(string name = "My Car")
{
await Task.Run(async () =>
{
// Get the 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 or capabilities are not checked.");
}
catch (Exception ex)
{
// Something else happened while acquiring the location.
MessageBox.Show(ex.Message);
}
});
}
//sets location of parking space using the GetCoordinates method
//opens map
private async void setLocationBtn_Click(object sender, RoutedEventArgs e)
{
await this.GetCoordinates();
NavigationService.Navigate(new Uri("/Maps.xaml", UriKind.Relative));
}
这是地图类,它什么都没做,有没有办法把前一个类的地理坐标传递给地图,然后画一个图钉?我想在OnNavigatedTo方法中这样做:
public partial class Maps: PhoneApplicationPage{Geolocator = null;Bool跟踪= false;ProgressIndicatorπ;MapLayer PushpinMapLayer;
public Maps()
{
InitializeComponent();
pi = new ProgressIndicator();
pi.IsIndeterminate = true;
pi.IsVisible = false;
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
}
}
添加图钉:
var overlay = new MapOverlay
{
PositionOrigin = new Point(0.5, 0.5),
GeoCoordinate = location, // takes a GeoCoordinate instance. convert Geoposition to GeoCoordinate
Content = new TextBlock{Text = "hello"}; // you can use any UIElement as a pin
};
var ml = new MapLayer { overlay };
map.Layers.Add(ml);
你可以在传递给NavigationSerivce.Navigate
的URI中附加你所在位置的经纬度作为查询,并在e.Uri.Query
的OnNavigatedTo
事件处理程序中提取它。
提示。Task.Run
将任务调度到线程池上运行。您的任务不受cpu限制,因此是task。运行不会给你带来任何性能提升。
将Geoposition转换为GeoCoordinate
var location = new GeoCoordinate(geoposition.Coordinate.Latitude, geoposition.Coordinate.Longitude);
一个非常有用的资源是Nokia WP8指南