我是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定位的R& D,我知道我需要添加" NSLocationAlwaysUsageDescription "到info.plist,但我没有得到如何添加这个。
getting-gps-location-using-core-location-in-ios-8
请帮助我如何获取IOS 8的GPS位置
答案 0 :(得分:2)
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()
答案 1 :(得分:0)
当您创建CLLocationmanager的新实例时,调用以下内容非常重要:
manager = new CLLocationManager();
1。)manager.RequestWhenInUseAuthorization(); //如果你在前台做任何事情,就会使用它。 2.)manager.RequestAlwaysAuthorization(); //如果你想在后台扫描,则使用它
当然这些只是iOS 8的方法,因此请务必使用以下方法进行版本检查:UIDevice.CurrentDevice.CheckSystemVersion(8,0);
这不足以提示您的用户访问位置。您现在需要修改Info.plist!您需要添加一个名为NSLocationWhenInUseUsageDescription或NSLocationAlwaysUsageDescription的新字符串条目。然后,您可以指定在尝试获取位置时提示用户的消息
答案 2 :(得分:-1)