Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pushpin is null

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

like image 877
Seige Avatar asked Aug 14 '14 22:08

Seige


1 Answers

I'm not sure what the problem is, but here are a couple of things to try:

  • use 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");
}
  • use the attached property reference to get the element (this would help if the problem is related somehow to namescope resolution):

public override void OnApplyTemplate()
{
    PushPin pin = MapExtensions.GetChildren(mapEventLocation)
        .FirstOrDefault() as Pushpin;
}
like image 125
McGarnagle Avatar answered Oct 20 '22 23:10

McGarnagle