2
votes

Using xamarin for current location currentLocation example I used the above Link for getting current location latitude and longitude.
In IOS 7 before upgrade to IOS 8 Its shows my current location latitude and longitude on both simulator and device.(Its looks strange for me in simulator for getting current location latitude and longitude).

After upgrade to iOS 8 and run same code it give

In simulator:(Turn on wifi no sim card location service App always.)

Result:
latitude = 37.785834.
longitude = -122.406517.

On Device IPhone5c (Turn on wifi no sim card location service App always.)

Result:
latitude = 37.785834.
longitude = -122.406517.

(But I expected my current location latitude and longitude )

After upgrade it does not ask, that application like use the Your Current Location GPS. an Alert message with YES, NO options.

How to get my current location latitude and longitude?

4
I have never had the simulator return any location other than the location set through debug or the location menu in the simulator. Check that you haven't forced a simulated location in Xcode debug - this will apply to both simulator and the devicePaulw11
When run the application on simulator its shows Result: latitude = 37.785834. longitude = -122.406517. When run on Device (Iphone5c) location services Location.IOS (Application Name) selected check Allow Location Access Always and open the app Result: Latitude : [lat] longitude: [lon].... Its does not work does not show current location latitude and longitude.KkMIW
I guess there is some thing wrong with locationManger. Unable to get current location latitude and longitude. Please let me know how to get current location latitude and longitude. using LocationManager xamarin for IOS.KkMIW
Hello Dear, Did you figured out this issue in simulator ?Anurag

4 Answers

6
votes

You need to request permission explicitly from iOS 8.0 onwards. The steps to do so:

  1. Update your Info.plist file. Insert a new key: either NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription keys and write the custom message that will be displayed when your app requests the permission.

It is a must to have either of these keys in your plist file otherwise the permission request won't be displayed! If you don't want to show a custom explanation text in the alertview, leave the key's value string empty, but you still must have the key present in the Info.plist file!

  1. Before you start tracking, check that you are on iOS8 then send either RequestAlwaysAuthorization or RequestWhenInUseAuthorization from the CLLocationManager instance. Example:

    var locationManager = new CLLocationManager();
    if (UIDevice.CurrentDevice.CheckSystemVersion (8, 0)) {
        locationManager.RequestAlwaysAuthorization();
    }
    

In this case, use the NSLocationAlwaysUsageDescription key in your Info.plist file. Please note, Xamarin Studio does not currently offer you this key automatically from the dropdown list, you have to enter / copy-paste this key manually there.

2
votes

When using the iOS simulator you need to set the custom location for the simulator hardware. Go to Debug, select location, custom location and then set the preferred co-ordinates.

0
votes

Add In LocationManger.cs file for method LocationManager

if (UIDevice.CurrentDevice.CheckSystemVersion (8, 0)) {
    this.locMgr.RequestAlwaysAuthorization();
}

After change

public LocationManager ()
{
    this.locMgr = new CLLocationManager();
    LocationUpdated += PrintLocation;
    if (UIDevice.CurrentDevice.CheckSystemVersion (8, 0)) {
        this.locMgr.RequestAlwaysAuthorization();
    }
}

Add Info Plist manually NSLocationAlwaysUsageDescription empty string.

Then clean the build and run the application. Its shows my current location latitude and longtidu.

0
votes

I tried above solutions but that did not work. Problem is when you are targeting IOS >= 9.0 then Location manager change some permission variables, which are supposed to mention explicitly in info.pList.

<key>NSLocationWhenInUseUsageDescription</key>
<string>Why you need Location</string>

<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Why you need Location</string>

<key>NSLocationAlwaysUsageDescription</key>
<string>Why you need Location</string>

<key>NSLocationWhileInUsage</key>
<string>Why you need Location</string>

use these four keys if you want to target all versions .. before and after 9.0.

Also note that this line means whether user is in foreground or background use locations (apple discourage this because it has potential of privacy) check documentation here

[locationManager requestAlwaysAuthorization];

whereas this means,

[locationManager requestWhenInUseAuthorization];

Only use when app is in foreground check documentation here