我在Core Location Services中遇到了本地化API的问题。我得到了请求对位置服务的许可的promt。如果我单击“允许”并转到“设置”,则可以看到我的应用程序没有权限。
这是我在GeoUtility类中的代码:
CLGeocoder *reverseGeocoder = [[CLGeocoder alloc] init];
CLLocationManager *lm = [[CLLocationManager alloc] init];
[lm setPurpose:@"Need to verify your region"];
[lm startUpdatingLocation];
它由viewDidAppear中的viewController触发
我还在plist文件中的Required Devices Capabilites下添加了location-services
。
答案 0 :(得分:0)
添加CoreLocation.framework和MapKit.framework
和强>
在.h
#import <MapKit/MapKit.h>
CLLocationManager *locationManager;
CLGeocoder *geocoder;
在视图中加载了
- (void)viewDidLoad
{
locationManager = [[CLLocationManager alloc] init];
geocoder = [[CLGeocoder alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
,然后强>
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
LongitudeLbl.text = [NSString stringWithFormat:@"%.8f",currentLocation.coordinate.longitude];
LatitudeLbl.text = [NSString stringWithFormat:@"%.8f",currentLocation.coordinate.latitude];
}
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
if (error == nil && [placemarks count] > 0) {
placemark = [placemarks lastObject];
NSString *addresstemp2 = [[NSString alloc] init];
NSString *subThoroughfare2 = [NSString stringWithFormat:@"%@",placemark.subThoroughfare];
NSString *thoroughfare2 = [NSString stringWithFormat:@"%@",placemark.thoroughfare];
NSString *postalCode2 = [NSString stringWithFormat:@"%@",placemark.postalCode];
NSString *locality2 = [NSString stringWithFormat:@"%@",placemark.locality];
NSString *administrativeArea2 = [NSString stringWithFormat:@"%@",placemark.administrativeArea];
NSString *country2 = [NSString stringWithFormat:@"%@",placemark.country];
if (![subThoroughfare2 isEqualToString:@"(null)"]) {
addresstemp2 = [NSString stringWithFormat:@"%@",subThoroughfare2];
}
if (![thoroughfare2 isEqualToString:@"(null)"]) {
addresstemp2 = [NSString stringWithFormat:@"%@ %@ \n",addresstemp2,thoroughfare2];
}
if (![postalCode2 isEqualToString:@"(null)"]) {
addresstemp2 = [NSString stringWithFormat:@"%@ %@",addresstemp2,postalCode2];
}
if (![locality2 isEqualToString:@"(null)"]) {
addresstemp2 = [NSString stringWithFormat:@"%@ %@ \n",addresstemp2,locality2];
}
if (![administrativeArea2 isEqualToString:@"(null)"]) {
addresstemp2 = [NSString stringWithFormat:@"%@ %@ \n",addresstemp2,administrativeArea2];
}
if (![country2 isEqualToString:@"(null)"]) {
addresstemp2 = [NSString stringWithFormat:@"%@ %@",addresstemp2,country2];
}
AddressLbl.text = [[NSString alloc] initWithString:addresstemp2];
[AddressLbl sizeToFit];
[locationManager stopUpdatingLocation];
} else {
}
} ];
}
您还可以更改模拟器中的设置转到设置,然后选择位置服务将其更改为开。如果它已关闭,则向下看是您的应用程序名称,然后更改为ON