【发布时间】:2014-07-22 13:20:07
【问题描述】:
我正在尝试使用纬度和经度通过 CLLocationManager 获取地址,但它只返回州和国家/地区名称,我还想要城市,我正在附上我的代码有人可以告诉我如何更改我的代码以获取城市名称.下面是我的代码
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
currentLocation = [locations objectAtIndex:0];
CLGeocoder *geocoder1 = [[CLGeocoder alloc] init];
NSLog(@"Detected Location : %f, %f", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
//CLLocation *newLocation = [[CLLocation alloc]initWithLatitude:26.9260 longitude:75.8235];
CLLocation *newLocation = [[CLLocation alloc]initWithLatitude:currentLocation.coordinate.latitude longitude:currentLocation.coordinate.longitude];
[geocoder1 reverseGeocodeLocation:newLocation
completionHandler:^(NSArray *placemarks, NSError *error) {
if (error) {
NSLog(@"Geocode failed with error: %@", error);
return;
}
if (placemarks && placemarks.count > 0)
{
CLPlacemark *placemark = placemarks[0];
NSDictionary *addressDictionary =
placemark.addressDictionary;
NSLog(@"%@ ", addressDictionary);
NSString *address = [addressDictionary
objectForKey:(NSString *)kABPersonAddressStreetKey];
NSString *city = [addressDictionary
objectForKey:(NSString *)kABPersonAddressCityKey];
NSString *state = [addressDictionary
objectForKey:(NSString *)kABPersonAddressStateKey];
NSString *zip = [addressDictionary
objectForKey:(NSString *)kABPersonAddressZIPKey];
NSLog(@"%@ %@ %@ %@", address,city, state, zip);
}
}];
}
【问题讨论】:
-
您并没有真正获得“通过 CLLocationManager 的地址”。您通过 CLGeocoder 获得它(CLLocationManager 只是给您坐标)。代码看起来不错。它可能特定于 CLGeocoder(Apple)可能无法很好地进行地理编码的坐标(Google 似乎在这方面做得更好)。坐标是 26.9260、75.8235 吗?
标签: ios cllocationmanager city