What should I do to get a not null LocationDisplay

1334
4
03-21-2017 10:42 AM
Marcelo_Cesar_AugustoTorres
New Contributor II

I'm developing a Xamarin.Forms application, and I need to get the Device Locations. I peek LocationDisplay of my MapView but always returns null. How I do to initialize the app to get GPS Loction from device?

0 Kudos
4 Replies
JenniferNery
Esri Regular Contributor

Have you enabled the location in the app manifest? At what point are you checking for LocationDisplay? It seems to always have a value (non-null).

You can try the following code:

             xmlns:esriUI="clr-namespace:Esri.ArcGISRuntime.Xamarin.Forms;assembly=Esri.ArcGISRuntime.Xamarin.Forms"
             x:Class="XamForms.MainPage">
    <esriUI:MapView x:Name="MyMapView" />

public MainPage()
{
    InitializeComponent();

    MyMapView.SpatialReferenceChanged += MyMapView_SpatialReferenceChanged;
    MyMapView.Map = new Map(Basemap.CreateTopographic());
}

private async void MyMapView_SpatialReferenceChanged(object sender, EventArgs e)
{
    try
    {
        if(!MyMapView.LocationDisplay.DataSource.IsStarted)
            await MyMapView.LocationDisplay.DataSource.StartAsync();
    }
    catch(Exception ex)
    {
        await DisplayAlert("Error", ex.Message, "OK");
    }
}
0 Kudos
Marcelo_Cesar_AugustoTorres
New Contributor II

Hello I did the test you wrote...always throws exception, tested with UWP (Local Machine) and Android (VS Android Simulator)...

I made a workaround using SystemLocationDataSource on Esri.ArcGISRuntime.Location namespace...

Thanks

0 Kudos
Marcelo_Cesar_AugustoTorres
New Contributor II

And yes I checked that the right permission was enabled on UWP and Android manifest files...

0 Kudos
marceloctorres
Esri Contributor

I forgot to mention that I am using the MVVM pattern, so in the ViewModel class I have a LocationDisplay property and in the XAML file I make the respective Binding to the ViewModel class property. But when I query the value of the property in the ViewModel I get the null value.

The issue is that LocationDisplay has no constructor, it can not be initialized by code and is the MapView that creates its LocationDisplay instance, but when doing Binding from the ViewModel I replace the value that the MapView gives to the LocationDisplay by a null value.

Solution: Add to the 'Binding the Mode = OneWayToSource' option, violá!!!

Marcelo César Torres
0 Kudos