Get GPS location in IOS 8 using XAMARIN

I'm new to Xamarin, I have a code to get a GPS location that works fine for IOS 6, but for IOS 8 it doesn't get long. and lat.

//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 ();
            }

      

I am using Generic Template to create an app in Xamarin. I have R&D for IOS 8 GPS location, I found out that I need to add " NSLocationAlwaysUsageDescription " to info.plist, but I don't understand how to add that.

getting-gps-location-using-core-location-in-ios-8

Please help me how to get GPS location for IOS 8

+3


source to share


3 answers


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

    }


}

      




This works in iOS8

I use this static class, every time before accepting coordinates I call Refresh () I can't find the thread where I found this solution, but it calls to return the location immediately and then call GetLocationResult (). to get the location and when you are done calling locationManager StopLocationManager ()

+2


source


When creating a new instance of CLLocationmanager, it is extremely important that you call the following:

manager = new CLLocationManager ();

1.) manager.RequestWhenInUseAuthorization (); // This is used if you are doing anything in the foreground. 2.) manager.RequestAlwaysAuthorization (); // This is used if you want to scan in the background



Of course, these are only iOS 8 methods, so be sure to check the version: UIDevice.CurrentDevice.CheckSystemVersion (8, 0);

This is not enough to offer the user access to the location. You will now need to change your Info.plist! You will need to add a new string entry called NSLocationWhenInUseUsageDescription or NSLocationAlwaysUsageDescription. Then you can specify the message you want to prompt your user when trying to get the location

0


source


-1


source







All Articles