100.6 LocationDisplay always null

1408
8
Jump to solution
09-03-2019 08:36 AM
CalebPourchot
New Contributor III

I upgraded to 100.6 for Forms. With no other changes I started crashing when displaying the map because I set LocationDisplay.Enabled to true. However, LocationDisplay is now always null. This is on both Android and iOS.

on iOS, I have Always Enabled location permissions on and they are working because I am getting location updates. 

on Android, I have location permissions on as well and they are working.

Please advise,

Caleb

Tags (1)
1 Solution

Accepted Solutions
ZackAllen
Esri Contributor

Hi Caleb! Thanks for asking about this issue. This is due to a behavior change in 100.6. You must wait for the LocationDisplay of your MapView to load before enabling it and changing it's properties. You can add an event handler for the PropertyChanged event in your MapView to enable your location display once it is loaded.

MyMapView.PropertyChanged += (o, e) =>
{
    if (e.PropertyName == nameof(MyMapView.LocationDisplay) && MyMapView.LocationDisplay != null)
    {
        // Enable your location display.
        MyMapView.LocationDisplay.IsEnabled = true;
    }
};‍‍‍‍‍‍‍

We use this pattern in our Show location history sample.

I hope that this helps,

Zack

View solution in original post

8 Replies
ZackAllen
Esri Contributor

Hi Caleb! Thanks for asking about this issue. This is due to a behavior change in 100.6. You must wait for the LocationDisplay of your MapView to load before enabling it and changing it's properties. You can add an event handler for the PropertyChanged event in your MapView to enable your location display once it is loaded.

MyMapView.PropertyChanged += (o, e) =>
{
    if (e.PropertyName == nameof(MyMapView.LocationDisplay) && MyMapView.LocationDisplay != null)
    {
        // Enable your location display.
        MyMapView.LocationDisplay.IsEnabled = true;
    }
};‍‍‍‍‍‍‍

We use this pattern in our Show location history sample.

I hope that this helps,

Zack

CalebPourchot
New Contributor III

Thank you Zack,

I appreciate this info. It would've been great if changes like these were

well noted in the release notes. If I missed it, I apologize.

All the best,

Caleb

dotMorten_esri
Esri Frequent Contributor

A slightly different/simpler approach is just to wait for the Page.OnAppearing event to fire. This event ensures the Xamarin.Forms renderers are correctly hooked up to the native underlying view. Generally I'd recommend to wait with doing too much to the MapView until this event fires.


Currently LocationDisplay isn't settable, which prevents us from linking it to the native view until that link has been established (we're hoping to make it settable so this specific change in behavior could go away). It was, believe it or not, a bug that there was an underlying native view prior to the appearing event, which caused a slew of life-cycle issues. that we now addressed in Update 6. Unfortunately you might see a couple of these behavioral changes due to us fixing this problem.

Good feedback that we should have documented this change in behavior. We'll try and get that addressed.

CalebPourchot
New Contributor III

I had it attached to the Map.MapLoaded event, so apparently that fires

before Page.OnAppearing. Is this guaranteed? Are there things that

shouldn't be touched if OnAppearing has fired, but the map isn't quite

loaded?

Thanks,

Caleb

0 Kudos
dotMorten_esri
Esri Frequent Contributor

It should be. I haven't seen a case where the underlying Forms renderers aren't hooked up at that point.

0 Kudos
CalebPourchot
New Contributor III

I recently switched to Page Appearing strategy and it is occasionally crashing - so far only on Android:

  • MapViewGenericViewModel.InitializeLocationDisplay ()
  • MapViewGenericViewModel.PageAppearing ()
  • AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state)
  • SyncContext+<>c__DisplayClass2_0.<Post>b__0 ()
  • Thread+RunnableImplementor.Run ()
  • IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this)
  • (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.63(intptr,intptr)

Null reference exception when trying to enable location display in my Page Appearing handler. I have to switch back to the original propertychanged method given above. This also makes me nervous about other things that aren't ready during Page Appearing...

Caleb

0 Kudos
MichaelHamsa
Occasional Contributor

This just hit me as well - I did not see anything in the release notes. Going to have to change a few things around to get this working again.

Mike...

msilvatavares
New Contributor

Hello guys,

I know that this is an old topic, but I'm trying to implement the functionalities of the MapView using the MVVM and even for the version 100.11, the LocationDisplay is returning null.

I would like to know if this issue was already fixed.

if so, could somebody provide some documentation/code snippet in how to initialize correctly the MapView base on MVVM implementation.

Thanks in advance,

Marcio

0 Kudos