【发布时间】:2013-04-05 00:16:10
【问题描述】:
我现在正在使用 Map Kit 和 Core Location,需要从 zip code 或 city/state 获取 location 信息。有什么办法吗?
【问题讨论】:
-
可以使用googlel api吗? Check this
标签: iphone ios objective-c ios5 mapkit
我现在正在使用 Map Kit 和 Core Location,需要从 zip code 或 city/state 获取 location 信息。有什么办法吗?
【问题讨论】:
标签: iphone ios objective-c ios5 mapkit
您可以使用CLGeocoder 类,它支持将地址转换为坐标,反之亦然。例如:
[geocoder geocodeAddressString:@"<postcode here>"
completionHandler:^(NSArray* placemarks, NSError* error){
for (CLPlacemark* aPlacemark in placemarks)
{
// Process the placemark.
}
}];
您可能想要使用许多不同的方法。例如,您可以将搜索限制在特定区域。
【讨论】:
您可以使用https://thezipcodes.com/ 从邮政编码获取位置。
要与您的网站集成,请关注http://thezipcodes.com/docs
【讨论】:
使用这个
-(CLLocationCoordinate2D) getLocationFromAddressString:(NSString*) addressStr {
double latitude = 0, longitude = 0;
NSString *esc_addr = [addressStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", esc_addr];
NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL];
if (result) {
NSScanner *scanner = [NSScanner scannerWithString:result];
if ([scanner scanUpToString:@"\"lat\" :" intoString:nil] && [scanner scanString:@"\"lat\" :" intoString:nil]) {
[scanner scanDouble:&latitude];
if ([scanner scanUpToString:@"\"lng\" :" intoString:nil] && [scanner scanString:@"\"lng\" :" intoString:nil]) {
[scanner scanDouble:&longitude];
}
}
}
CLLocationCoordinate2D center;
center.latitude = latitude;
center.longitude = longitude;
return center;
}
当您在 this 中传递地址作为参数时,此方法使用 google api 返回 坐标。
【讨论】: