问题描述
如何获取当前位置的纬度和经度。
how to get current location latitude and longitude.
我试过这个。使用Xcode 6.01和IOS SDK 8.0
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];
}
这里我将模拟器位置启用到Deployment 8.0 iPhone模拟器4s 。
即使在设备上也无法正常工作。
Here I enable the simulator location to Deployment 8.0 iPhone simulator 4s.Even on device its not working.
@ALL
请告诉我这里我做错了什么。
@ALLPlease let me know, what i have done wrong here.
推荐答案
我在IOS 8.0中找出问题
I figure it out issue in IOS 8.0
NSLocationAlwaysUsageDescription 将此手动添加到应用程序plist文件中,将字符串值保留为空。
这将调用委托方法。永远不要忘记添加 NSLocationAlwaysUsageDescription ,除非您没有添加此委托方法将无法正常工作。剩下的代码是一样的。将委托方法添加到控制器类。
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.
在locationManager声明代码中添加此行代码。对于IOS 8.0
[locationManager requestAlwaysAuthorization];
add this line of code in locationManager declaration code. for IOS 8.0 [locationManager requestAlwaysAuthorization];
这篇关于如何在IOS SDK 8.0中获取当前位置的经度和纬度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!