我正在尝试使用CLLocationManager和CLGeocoder来确定iPhone用户的位置。
我的CLLocationManager似乎运行良好,但遗憾的是CLGeocoder并没有按照我的意愿去做。
部分代码摘录:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
[self.locationManager stopUpdatingLocation];
NSLog(@"Location updated to: %@",newLocation);
[self.geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"Reverse geocoding finished");
self.currentPlacemark = [placemarks objectAtIndex:0];
NSLog(@"%@",self.currentPlacemark.locality);
NSLog(@"%@",self.currentPlacemark.ISOcountryCode);
}];
}
- (void)getCurrentLocation {
NSLog(@"Determining current location");
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self getCurrentLocation];
}
self.currentPlacemark 是 CLPlacemark 对象。
self.geocoder 是 CLGeocoder 对象。
self.locationManager 是 CLLocationManager 对象。
我错过了什么?为什么这不起作用? 老实说,我花了好几个小时,我慢慢开始变得绝望......
答案 0 :(得分:3)
是的,您应该分配并初始化地理编码器。
self.geocoder = [[[CLGeocoder alloc] init] autorelease];
[self.geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"Reverse geocoding finished");
self.currentPlacemark = [placemarks objectAtIndex:0];
NSLog(@"%@",self.currentPlacemark.locality);
NSLog(@"%@",self.currentPlacemark.ISOcountryCode);
}];