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;
}