如何从reverseGeocodeCoordinate获取国家,州,城市? [英] How to obtain country, state, city from reverseGeocodeCoordinate?
问题描述
GMSReverseGeocodeResponse
包含
- (GMSReverseGeocodeResult *)firstResult;
其定义如下:
@interface GMSReverseGeocodeResult : NSObject<NSCopying>
/** Returns the first line of the address. */
- (NSString *)addressLine1;
/** Returns the second line of the address. */
- (NSString *)addressLine2;
@end
有什么方法可以从这两个字符串(对所有国家/地区和所有地址有效)中获取国家/地区,ISO国家/地区代码,州(administrative_area_1或对应的国家/地区) >)?
Is there any way to obtain the country, ISO country code, state (administrative_area_1 or corresponding one) from those two strings (valid for all the countries and all the addresses)?
注意:我试图执行这段代码
NOTE: I tried to execute this piece of code
[[GMSGeocoder geocoder] reverseGeocodeCoordinate:CLLocationCoordinate2DMake(40.4375, -3.6818) completionHandler:^(GMSReverseGeocodeResponse *resp, NSError *error)
{
NSLog( @"Error is %@", error) ;
NSLog( @"%@" , resp.firstResult.addressLine1 ) ;
NSLog( @"%@" , resp.firstResult.addressLine2 ) ;
} ] ;
但是由于某种原因,从未调用过该处理程序.我确实添加了应用程序密钥,还向应用程序密钥添加了iOS捆绑软件ID.控制台中未打印任何错误.我的意思是我不知道这些行的内容.
But for some reason the handler was never called. I did add the app key, and also added the iOS bundle id to the app key. No error is printed in the console. With this I mean I am not aware of the content of the lines.
推荐答案
最简单的方法是升级到发行说明:
The simplest way is to upgrade to Version 1.7 of the Google Maps SDK for iOS (released February 2014).
From the release notes:
GMSGeocoder
现在通过GMSAddress
提供了结构化地址,已弃用GMSReverseGeocodeResult
.
GMSGeocoder
now provides structured addresses viaGMSAddress
, deprecatingGMSReverseGeocodeResult
.
在 GMSAddress
类引用中,您可以找到这些属性:
coordinate
位置,或者kLocationCoordinate2DInvalid
(如果未知).
coordinate
Location, orkLocationCoordinate2DInvalid
if unknown.
thoroughfare
街道号和名称.
thoroughfare
Street number and name.
locality
地区或城市.
locality
Locality or city.
subLocality
细分地区,区域或公园.
subLocality
Subdivision of locality, district or park.
administrativeArea
地区/州/行政区域.
administrativeArea
Region/State/Administrative area.
postalCode
邮政编码.
postalCode
Postal/Zip code.
country
国名.
country
The country name.
lines
NSString
的数组,其中包含地址的格式化行.
lines
An array of NSString
containing formatted lines of the address.
虽然没有ISO国家/地区代码.
另请注意,某些属性可能返回nil
.
No ISO country code though.
Also note that some properties may return nil
.
这是一个完整的例子:
[[GMSGeocoder geocoder] reverseGeocodeCoordinate:CLLocationCoordinate2DMake(40.4375, -3.6818) completionHandler:^(GMSReverseGeocodeResponse* response, NSError* error) {
NSLog(@"reverse geocoding results:");
for(GMSAddress* addressObj in [response results])
{
NSLog(@"coordinate.latitude=%f", addressObj.coordinate.latitude);
NSLog(@"coordinate.longitude=%f", addressObj.coordinate.longitude);
NSLog(@"thoroughfare=%@", addressObj.thoroughfare);
NSLog(@"locality=%@", addressObj.locality);
NSLog(@"subLocality=%@", addressObj.subLocality);
NSLog(@"administrativeArea=%@", addressObj.administrativeArea);
NSLog(@"postalCode=%@", addressObj.postalCode);
NSLog(@"country=%@", addressObj.country);
NSLog(@"lines=%@", addressObj.lines);
}
}];
及其输出:
coordinate.latitude=40.437500
coordinate.longitude=-3.681800
thoroughfare=(null)
locality=(null)
subLocality=(null)
administrativeArea=Community of Madrid
postalCode=(null)
country=Spain
lines=(
"",
"Community of Madrid, Spain"
)
或者,您可以考虑在
Alternatively, you may consider using Reverse Geocoding in the The Google Geocoding API (example).
这篇关于如何从reverseGeocodeCoordinate获取国家,州,城市?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!