我正在尝试使用一个iPhone和CLGeocoder来确定CLLocationManager用户的位置。
我的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对象。
我遗漏了什么?为什么这不管用?我真的花了几个小时慢慢地开始变得绝望的…
发布于 2012-06-08 11:24:15
是的,你应该分配和插入你的地理编码器。
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);
}];https://stackoverflow.com/questions/10947762
复制相似问题