gpt4 book ai didi

ios - 如何从reverseGeocodeCoordinate获取国家,州,城市?

转载 作者:IT王子 更新时间:2023-10-29 07:58:44 28 4
gpt4 key购买 nike

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

有没有办法从这两个字符串(对所有国家所有地址)?

注意:我尝试执行这段代码

[[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 ) ;
} ] ;

但由于某种原因,处理程序从未被调用过。我确实添加了应用程序 key ,并将 iOS 包 ID 添加到应用程序 key 。控制台中未打印任何错误。我的意思是我不知道这些行的内容。

最佳答案

最简单的方法是升级到1.7 版 Google Maps SDK for iOS (2014 年 2 月发布)。
来自release notes :

GMSGeocoder now provides structured addresses via GMSAddress, deprecating GMSReverseGeocodeResult.

来自 GMSAddress Class Reference , 你可以找到 these properties :

coordinate
Location, or kLocationCoordinate2DInvalid if unknown.

thoroughfare
Street number and name.

locality
Locality or city.

subLocality
Subdivision of locality, district or park.

administrativeArea
Region/State/Administrative area.

postalCode
Postal/Zip code.

country
The country name.

lines
An array of NSString containing formatted lines of the address.

虽然没有 ISO 国家代码。
另请注意,某些属性可能会返回 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"
)

或者,您可以考虑使用 Reverse GeocodingThe Google Geocoding API (example)。

关于ios - 如何从reverseGeocodeCoordinate获取国家,州,城市?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15036007/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com