【问题标题】:Get GPS Location in IOS 8 using XAMARIN使用 XAMARIN 在 IOS 8 中获取 GPS 位置
【发布时间】:2014-12-12 11:29:52
【问题描述】:
我是 Xamarin 的新手,我有一个获取 GPS 位置的代码,该代码在 IOS 6 上运行良好,但对于 IOS 8,它无法获取很长时间。和纬度。
//Check for getting current lat/long
CLLocationCoordinate2D toLocation;
private CLLocationManager locationManager;
if (!CLLocationManager.LocationServicesEnabled) {
AppDelegate.currLat = "";
AppDelegate.currLong = "";
gpsFlag = false;
} else {
gpsFlag = true;
locationManager = new CLLocationManager();
locationManager.StartUpdatingLocation();
if (locationManager.Location != null) {
toLocation = locationManager.Location.Coordinate;
AppDelegate.currLat = Convert.ToString (toLocation.Latitude);
AppDelegate.currLong = Convert.ToString (toLocation.Longitude);
}
locationManager.StopUpdatingLocation ();
}
我正在使用通用模板在 Xamarin 中创建应用程序。我有 IOS 8 GPS 定位的研发,我知道我需要将“NSLocationAlwaysUsageDescription”添加到 info.plist,但我不知道如何添加。
getting-gps-location-using-core-location-in-ios-8
请帮助我如何获取 IOS 8 的 GPS 位置
【问题讨论】:
标签:
ios
iphone
xamarin.ios
gps
xamarin
【解决方案1】:
public class LocationHelper
{
private static bool _isTracking;
public static bool IsTracking { get { return _isTracking; } }
private static string _longitude;
private static string _latitude;
private static DateTime _lastUpdated;
public static event EventHandler LocationUpdated;
public static CLLocationManager LocationManager { private set; get; }
public static void StartLocationManager(double distanceFilter, double accuracy)
{
LocationManager = new CLLocationManager();
if (LocationManager.RespondsToSelector(new MonoTouch.ObjCRuntime.Selector("requestWhenInUseAuthorization")))
LocationManager.RequestWhenInUseAuthorization();
LocationManager.DistanceFilter = CLLocationDistance.FilterNone;
LocationManager.DesiredAccuracy = accuracy;
LocationManager.LocationsUpdated += LocationManager_LocationsUpdated;
LocationManager.StartUpdatingLocation();
_isTracking = true;
System.Diagnostics.Debug.WriteLine("Location manager started ");
}
public static void StopLocationManager()
{
if (LocationManager != null)
{
LocationManager.LocationsUpdated -= LocationManager_LocationsUpdated;
LocationManager = null;
_isTracking = false;
}
}
public static void Refresh()
{
LocationManager.StopUpdatingLocation();
LocationManager.StartUpdatingLocation();
}
private static void LocationManager_LocationsUpdated(object sender, CLLocationsUpdatedEventArgs e)
{
if (LocationUpdated != null)
LocationUpdated(null, null);
UpdateLocation(e.Locations[e.Locations.Length - 1]);
}
private static void UpdateLocation(CLLocation location)
{
_longitude = location.Coordinate.Longitude.ToString();
_latitude = location.Coordinate.Latitude.ToString();
_lastUpdated = DateTime.Now;
}
public static LocationResult GetLocationResult()
{
return new LocationResult(_latitude, _longitude, _lastUpdated);
}
public class LocationResult
{
public DateTime UpdatedTime { private set; get; }
public string Latitude { private set; get; }
public string Longitude { private set; get; }
public LocationResult(string latitude, string longitude, DateTime updated)
{
UpdatedTime = updated;
Latitude = latitude;
Longitude = longitude;
}
}
}
这适用于 iOS8
我正在使用这个静态类,每次在获取坐标之前我都会调用 Refresh() 我找不到找到此解决方案的线程,但这会导致立即返回位置,然后调用 GetLocationResult() 以获取位置,当你完成 locationManager 调用 StopLocationManager()
【解决方案2】:
当您创建 CLLocationmanager 的新实例时,调用以下命令非常重要:
manager = new CLLocationManager();
1.) manager.RequestWhenInUseAuthorization(); //如果您在前台执行任何操作,则使用此选项。
2.) manager.RequestAlwaysAuthorization();//如果要在后台扫描时使用这个
当然,这些是 iOS 8 唯一的方法,因此请确保使用以下命令进行版本检查: UIDevice.CurrentDevice.CheckSystemVersion (8, 0);
但这还不足以提示您的用户访问位置。您现在需要修改 Info.plist!您将需要添加一个名为 NSLocationWhenInUseUsageDescription 或 NSLocationAlwaysUsageDescription 的新字符串条目。然后,您可以指定在尝试获取位置时要提示用户的消息