问题
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