How to get current location latitude and longitude in IOS SDK 8.0

丶灬走出姿态 提交于 2019-12-18 15:54:36

问题


how to get current location latitude and longitude.

I have try this. Using Xcode 6.01 and IOS SDK 8.0

-(CLLocationCoordinate2D) getLocation{
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    [locationManager startUpdatingLocation];
    CLLocation *location = [locationManager location];
    CLLocationCoordinate2D coordinate = [location coordinate];
    return coordinate;
}

- (void)getCurrentLocation{    
    CLLocationCoordinate2D coordinate = [self getLocation];
    NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude];
    NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude];
    NSLog(@"Latitude  = %@", latitude);
    NSLog(@"Longitude = %@", longitude);
}


- (void)viewDidLoad{
    [super viewDidLoad];
    [self getCurrentLocation];

}

Here I enable the simulator location to Deployment 8.0 iPhone simulator 4s. Even on device its not working.

@ALL Please let me know, what i have done wrong here.


回答1:


I figure it out issue in IOS 8.0

NSLocationAlwaysUsageDescription add this manually into application plist file, leave string value empty. This it will call the delegate methods. Never forgot about adding NSLocationAlwaysUsageDescription unless you not added this delegate method will not work. Remaining code is same. with adding delegate methods to controller class.

add this line of code in locationManager declaration code. for IOS 8.0 [locationManager requestAlwaysAuthorization];




回答2:


In iOS 8, this code fails silently i.e you won’t get any error or warning.

You need to do two extra things to get location working:

  1. Add a key to your Info.plist
  2. Request authorization from the location manager asking it to start.

Any one or both of below keys needs to be added in Info.plist file.

  1. NSLocationWhenInUseUsageDescription
  2. NSLocationAlwaysUsageDescription

These are of String type and value can be any message description or empty.

Now you need to request authorisation for corresponding location method. Use any one of below calls :

  1. [self.locationManager requestWhenInUseAuthorization]
  2. [self.locationManager requestAlwaysAuthorization]



回答3:


Yes, the simulator offers an over-simplified rendition of location services. A physical device is the only way to really test CLLocationManager behavior.

The issue is that when you call startUpdatingLocation, it doesn't mean that you can immediately retrieve the location. (I think you'll find it's either nil or has a negative horizontalAccuracy, which means the location has not yet been determined.)

You must conform to the CLLocationManagerDelegate, and implement locationManager:didUpdateLocations:, which is called asynchronously. So, you have to wait for this method to be called before trying to use the location information. And, note, on a real device, you will likely see this method called a number times as the device refines the device location information, with increasing accuracy (e.g. you may see the horizontalAccuracy number dropping from one location update to another).


Obviously, you probably want to implement locationManager:didFailWithError:, too, so problems can be detected. You probably should also to check authorizationStatus, locationServicesEnabled, and call requestWhenInUseAuthorization, before calling startUpdatingLocation.




回答4:


It appears we need to do some more task to implement location services in ios8. follow this discussion



来源:https://stackoverflow.com/questions/26147984/how-to-get-current-location-latitude-and-longitude-in-ios-sdk-8-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!