简体   繁体   中英

Get device location in latitude-longitude in xamarin forms

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.

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

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;
            }

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;
                        }
                    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM