Google iOS 地理编码 SDK 将经纬度反向转换为 Places ID
Posted
技术标签:
【中文标题】Google iOS 地理编码 SDK 将经纬度反向转换为 Places ID【英文标题】:Google iOS Geocoding SDK reverse lat long to Places ID 【发布时间】:2015-06-02 06:05:25 【问题描述】:目前,我们可以使用 GeoCoder 中的 reverseGeocodeCoordinate 将经纬度反转为地址。
https://developers.google.com/maps/documentation/ios/reference/interface_g_m_s_geocoder
但是,似乎无法将其反转到 Google 地方。
是否可以从 iOS SDK 获取地点?
谢谢 亚历克斯
【问题讨论】:
【参考方案1】:是的,您可以使用 iOS SDK 的 CLGeocoder。检查代码,
+ (void)fetchAddressFromCoordinates:(CoordinatesDataModel *)coordinates returnValue:(void(^)(NSString* address))returnBlock
CLLocation *currentLocation = [[CLLocation alloc] initWithLatitude:(CLLocationDegrees)[coordinateDataModel.latitude doubleValue] longitude:(CLLocationDegrees)[coordinateDataModel.longitude doubleValue]];
if (currentLocation != nil)
// Reverse Geocoding
// “reverseGeocodeLocation” method to translate the locate data into a human-readable address.
// The reason for using "completionHandler" ----
// Instead of using delegate to provide feedback, the CLGeocoder uses “block” to deal with the response. By using block, you do not need to write a separate method. Just provide the code inline to execute after the geocoding call completes.
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error)
if (error == nil && [placemarks count] > 0)
if(placemarks && placemarks.count > 0)
CLPlacemark *placemark= [placemarks objectAtIndex:0];
// address defined in .h file
NSString *address = [NSString stringWithFormat:@"%@ %@ %@ %@ %@",[placemark subThoroughfare], [placemark thoroughfare], [placemark locality], [placemark administrativeArea], [placemark country]];
NSCharacterSet *whitespace = [NSCharacterSet whitespaceCharacterSet];
address = [address stringByTrimmingCharactersInSet:whitespace];
NSMutableArray *words = [[address componentsSeparatedByCharactersInSet:whitespace] mutableCopy];
// NSLog(@":%@",words);
NSMutableArray * wordsArray = [[NSMutableArray alloc] init];
for (int i= 0; i< [words count]; i++)
if (![[words objectAtIndex:i] isEqualToString:@"(null)"] )
[wordsArray addObject:[words objectAtIndex:i]];
address = [wordsArray componentsJoinedByString:@" "];
returnBlock(address);
];
【讨论】:
谢谢,但是可以使用 Google 地方信息吗? 查看this。 谢谢,检查。所以你的意思是我需要先获取地址,然后再调用另一个请求来获取 Google 地方数据?以上是关于Google iOS 地理编码 SDK 将经纬度反向转换为 Places ID的主要内容,如果未能解决你的问题,请参考以下文章
猫猫学iOS 之CoreLocation反地理编码小Demo输入经纬度得到城市