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?
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. 😕
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á!!!
And yes I checked that the right permission was enabled on UWP and Android manifest files...
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
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"> <SPAN class="token tag"><SPAN class="token tag"><SPAN class="punctuation token"><</SPAN><SPAN class="namespace token">esriUI:</SPAN>MapView</SPAN> <SPAN class="attr-name token"><SPAN class="namespace token">x:</SPAN>Name</SPAN><SPAN class="attr-value token"><SPAN class="punctuation token">=</SPAN><SPAN class="punctuation token">"</SPAN>MyMapView<SPAN class="punctuation token">"</SPAN></SPAN> <SPAN class="punctuation token">/></SPAN></SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
<SPAN class="keyword token">public</SPAN> <SPAN class="token function">MainPage</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="token function">InitializeComponent</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> MyMapView<SPAN class="punctuation token">.</SPAN>SpatialReferenceChanged <SPAN class="operator token">+</SPAN><SPAN class="operator token">=</SPAN> MyMapView_SpatialReferenceChanged<SPAN class="punctuation token">;</SPAN> MyMapView<SPAN class="punctuation token">.</SPAN>Map <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">Map</SPAN><SPAN class="punctuation token">(</SPAN>Basemap<SPAN class="punctuation token">.</SPAN><SPAN class="token function">CreateTopographic</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="keyword token">private</SPAN> <SPAN class="keyword token">async</SPAN> <SPAN class="keyword token">void</SPAN> <SPAN class="token function">MyMapView_SpatialReferenceChanged</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="keyword token">object</SPAN> sender<SPAN class="punctuation token">,</SPAN> EventArgs e<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="keyword token">try</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="keyword token">if</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="operator token">!</SPAN>MyMapView<SPAN class="punctuation token">.</SPAN>LocationDisplay<SPAN class="punctuation token">.</SPAN>DataSource<SPAN class="punctuation token">.</SPAN>IsStarted<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">await</SPAN> MyMapView<SPAN class="punctuation token">.</SPAN>LocationDisplay<SPAN class="punctuation token">.</SPAN>DataSource<SPAN class="punctuation token">.</SPAN><SPAN class="token function">StartAsync</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="keyword token">catch</SPAN><SPAN class="punctuation token">(</SPAN>Exception ex<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="keyword token">await</SPAN> <SPAN class="token function">DisplayAlert</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Error"</SPAN><SPAN class="punctuation token">,</SPAN> ex<SPAN class="punctuation token">.</SPAN>Message<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"OK"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="punctuation token">}</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Les membres connectés peuvent publier, suivre les mises à jour, et plus encore. Nouveau ici ? Inscrivez-vous gratuitement.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.