问题
As the title suggest I am struggeling with the display of the current location of the user on the map.
Afaik there a two ways to display the current user location:
In Code: create a ellipse, set it as content of a
MapOverlay
, set the GeoCoordinate of the MapOverlay, add this overlay to aMapLayer
and add this to the map - but I cannot access theMapOverlay
anymore and thus I cannot change (update) the position of the user. Is there a way to update the user position without having to redraw all the map layers?XAML and Code: In Windows Phone 7 I was able to add a
Pushpin
to the map and let theGeoCoordinateWatcher
handle thePositionChanged
event and set theMyLocation.Location
to the current location.
It seems that there is no way to add a Pushpin
to the map if you don't use the Windows Phone Toolkit. So I tried to add Pushpin
from the Windows Phone Toolkit:
xmlns:maps="clr-namespace:Microsoft.Phone.Maps.Controls;assembly=Microsoft.Phone.Maps"
xmlns:maptk="clr-namespace:Microsoft.Phone.Maps.Toolkit;assembly=Microsoft.Phone.Controls.Toolkit"
<maptk:Pushpin x:Name="MyLocation">
<maptk:Pushpin.Template>
<ControlTemplate>
<Ellipse
Width="15"
Height="15"
Margin="0"
Fill="SteelBlue"
StrokeThickness="1"
Stroke="Black"/>
</ControlTemplate>
</maptk:Pushpin.Template>
</maptk:Pushpin>
But I cannot access the MyLocation pushpin in my MapPage.xaml (the debugger says it is null
).
Can I display and update the current user position on the map with maptk:Pushpin
?
In Windows Phone 7 I could simply create a XAML layout for the pushpin which would hold the user location like this:
xmlns:maps="clr-namespace:Microsoft.Phone.Controls.Maps;assembly=Microsoft.Phone.Controls.Maps"
<maps:Map Grid.Row="2" x:Name="Map" >
<maps:Pushpin x:Name="MyLocation" >
<maps:Pushpin.Template>
<ControlTemplate>
<Ellipse Width="12"
Height="12"
Margin="0"
Fill="SteelBlue"
StrokeThickness="1"
Stroke="Black"
/>
</ControlTemplate>
</maps:Pushpin.Template>
</maps:Pushpin>
</maps:Map>
then I could add a GeoCoordinateWatcher
which would listen for the PositionChanged
event and change the Location
of the MyLocation pushpin:
void GeoCoordinateWatcher_PositionChanged ( object sender, GeoPositionChangedEventArgs<GeoCoordinate> e )
{
MyLocation.Location = e.Position.Location;
}
回答1:
try this way,
Pushpin pushpin = new Pushpin();
pushpin.Background = new SolidColorBrush(Colors.Red);
pushpin.Location = new GeoCoordinate(Latitude, Longitude);
pushpin.Content = "SampleString";
pushpin.Width = 25;
map1.Children.Add(pushpin);//this is way add pushpin to map
Have visited this one .
http://social.msdn.microsoft.com/Forums/wpapps/en-US/3d95b0c5-5f48-4918-b761-f8302fa090c2/updatehow-to-show-pushpin-on-microsoftphonemapscontrolsmap#bbb42943-663b-41f1-a920-f7368f2fee70
来源:https://stackoverflow.com/questions/18977613/windows-phone-8-howto-display-current-position-on-the-map