Ram Singh
Ram Singh

Reputation: 6918

Get GPS Location in IOS 8 using XAMARIN

i am new to Xamarin, i have a code to get GPS location, which is working fine for IOS 6 but for IOS 8 it is not fetching the 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 the Universal Template to create Application in Xamarin. i have R&D for IOS 8 GPS location, i got to know that i need to add "NSLocationAlwaysUsageDescription" to info.plist, but i am not getting how to add this.

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

Please help me how can get the GPS location for IOS 8

Upvotes: 3

Views: 6358

Answers (2)

JamesMontemagno
JamesMontemagno

Reputation: 3792

When you create 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 iOS 8 only methods so makes sure to do a version check with: UIDevice.CurrentDevice.CheckSystemVersion (8, 0);

This isn’t enough though to prompt your user for access to location. You will need to now modify 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 with when attempting to get location

Upvotes: 0

user3455363
user3455363

Reputation: 418

 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'm using this static class, every time before taking coordinates I'm calling Refresh() I'm cant find thread where i found this solution but this causes to return location immediately, and then call to GetLocationResult() to get location and when u finished with locationManager call StopLocationManager()

Upvotes: 2

Related Questions