I have a map with 2 Pushpins, when I load the page, I am setting the location of one of the pushpins in the code behind. However, when I try to reference the pin, it is null.
XAML
<maps:Map x:Name="mapEventLocation" ZoomLevel="15" Grid.Row="1" Tap="mapEventLocation_Tap" >
<maptoolkit:MapExtensions.Children>
<maptoolkit:Pushpin x:Name="userLocationPin" Content="You" Visibility="Collapsed" />
<maptoolkit:Pushpin x:Name="eventLocationPin" Content="Event" Visibility="Collapsed" />
</maptoolkit:MapExtensions.Children>
</maps:Map>
C#
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
if (this.NavigationContext.QueryString.ContainsKey("lat"))
{
userLocationPin.GeoCoordinate = new GeoCoordinate(double.Parse(this.NavigationContext.QueryString["lat"]), double.Parse(this.NavigationContext.QueryString["lon"]));
userLocationPin.Visibility = System.Windows.Visibility.Visible;
mapEventLocation.Center = new GeoCoordinate(double.Parse(this.NavigationContext.QueryString["lat"]), double.Parse(this.NavigationContext.QueryString["lon"]));
}
}
Lat and Lon are definitely in the QueryString.
Before this, used
Pushpin pin = (Pushpin)this.FindName("userLocationPin");
That works for the emulator, but not on the real phone.
I have tried to make the Pushpins manually in a layer, but the pin goes off to the corner
Pushpin userLocation = new Pushpin();
userLocation.GeoCoordinate = new GeoCoordinate(double.Parse(this.NavigationContext.QueryString["lat"]), double.Parse(this.NavigationContext.QueryString["lon"]));
userLocation.Content = "You";
MapLayer layer = new MapLayer();
MapOverlay overlay = new MapOverlay();
overlay.Content = userLocation;
layer.Add(overlay);
mapEventLocation.Layers.Add(layer);
Any ideas would be greatly appreciated. Thanks
I'm not sure what the problem is, but here are a couple of things to try:
OnApplyTemplate
rather than Loaded
(this would help if the problem is the timing of how the child templates are being loaded/applied):public override void OnApplyTemplate()
{
Pushpin pin = (Pushpin)this.FindName("userLocationPin");
}
public override void OnApplyTemplate()
{
PushPin pin = MapExtensions.GetChildren(mapEventLocation)
.FirstOrDefault() as Pushpin;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With