如何在iphone中获取地理位置城市名称?
Posted
技术标签:
【中文标题】如何在iphone中获取地理位置城市名称?【英文标题】:how to get geolocation city name in iphone? 【发布时间】:2012-11-21 05:09:01 【问题描述】:我已尝试使用此代码获取基于地理位置的值,但无法获取城市名称。如何获得城市名称?
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
double lati = newLocation.coordinate.latitude;
_geo_coder_latitude_lbl.text= [[NSString stringWithFormat:@"%f",lati] retain];
_geocoder_latitude_str=_geo_coder_latitude_lbl.text;
NSLog(@"print lat;%@",_geocoder_latitude_str);
double longi = newLocation.coordinate.longitude;
_geocoder_longitude_lbl.text= [[NSString stringWithFormat:@"%f",longi] retain];
_geocoder_longitude_str=_geocoder_longitude_lbl.text;
NSLog(@"print lat;%@",_geocoder_longitude_str);
[self._geocoder reverseGeocodeLocation: locationManager.location completionHandler:
^(NSArray *placemarks, NSError *error)
//Get address
CLPlacemark *placemark = [placemarks objectAtIndex:0];
NSLog(@"Placemark array: %@",placemark.addressDictionary );
//String to address
_located_address = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
//Print the location in the console
NSLog(@"Currently address is: %@",_located_address);
// _ex_map_address_lbl.text=_located_address;
];
[self _storeList_json_parser];
【问题讨论】:
【参考方案1】:查看 CLPlacemark docs,[CLPlacemark locatity]
将返回与地标关联的城市名称。
查看此示例代码:
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
double lati = 45.46433;
double longi = 9.18839;
CLLocation *location = [[CLLocation alloc] initWithCoordinate:CLLocationCoordinate2DMake(lati, longi)
altitude:0
horizontalAccuracy:0
verticalAccuracy:0
timestamp:[NSDate date]];
[geocoder reverseGeocodeLocation:location completionHandler:
^(NSArray *placemarks, NSError *error)
CLPlacemark *placemark = [placemarks lastObject];
if (error || !placemark)
return;
NSString *city = placemark.locality;
if (!city)
city = placemark.subAdministrativeArea;
NSLog(@"City for location: %@", city);
];
【讨论】:
是的,我曾经尝试过这个 placemark.locality 有时会出现城市名称,但有时 placemark.locality 响应显示空值。我怎样才能获得城市名称的标准响应。 如果您使用 subAdministrativeArea 回退怎么办?查看更新的代码。 很好,但某些地标字段响应显示空值。例如,它显示:地标地点:哥印拜陀地标子区域:(空)地标子管理区域:(空)地标通道:Siddhapudur Road Placemark subThoroughfare:(空) ) 地标名称:Siddhapudur Road 地标国家:印度地标行政区域:泰米尔纳德邦市,位置:哥印拜陀寺,它有时会显示空值的原因是什么? MKReverseGeocoder 对于某些位置可能不可靠。见bit.ly/Senltl以上是关于如何在iphone中获取地理位置城市名称?的主要内容,如果未能解决你的问题,请参考以下文章