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?
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");
}
}
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
And yes I checked that the right permission was enabled on UWP and Android manifest files...
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á!!!
Yes it is nice but ther is a problem that binding will happen after a while after OnAppearingEvent start. And you do not know wehen you are able to use it. 😕