Get device location in latitude-longitude in xamarin forms

喜你入骨 提交于 2020-01-04 04:11:21

问题


I have a scanner in my application, and as I am scanning any QR code I need to get a current location of device in latitude-longitude. I don't have any idea how to get location so I don't have any piece of code right now. Suggest me some ways to get location on scanning completion of QR code.


回答1:


Geolocator Plugin example:

var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 50;
var position = await locator.GetPositionAsync (timeoutMilliseconds: 10000);
Console.WriteLine ("Position Status: {0}", position.Timestamp);
Console.WriteLine ("Position Latitude: {0}", position.Latitude);
Console.WriteLine ("Position Longitude: {0}", position.Longitude);

Ref: https://github.com/jamesmontemagno/GeolocatorPlugin

Nuget: https://www.nuget.org/packages/Xam.Plugin.Geolocator




回答2:


For everyone that is confused about this location stuff I recommend using Xamarin Essentials and not the plug-in mentioned above.

            var location = await Geolocation.GetLastKnownLocationAsync();                                
            if (location != null)
            {
                MyLocation = location;
                if (!ShowUserLocation)
                    ShowUserLocation = true;
            }



回答3:


I recommend Xamarin Essentials. Look this post and check your permissions. https://docs.microsoft.com/en-us/xamarin/essentials/geolocation?tabs=android

var Latitude;
var Longitude;    
var request = new GeolocationRequest(GeolocationAccuracy.Best);
                    var location = await Geolocation.GetLocationAsync(request);

                    if (location != null)
                    {
                        if (location.IsFromMockProvider)
                        {
                            //Put a message if detect a mock location.
                        }else
                        {
                            Latitude=location.Latitude;
                            Longitude=location.Longitude;
                        }
                    }


来源:https://stackoverflow.com/questions/38137560/get-device-location-in-latitude-longitude-in-xamarin-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!