【发布时间】:2014-03-02 23:07:32
【问题描述】:
我根据经度和纬度值获得了当前位置 然后我还使用注释在谷歌地图上获得了多个地方 现在我想根据邮政编码获取经度和纬度值 我不知道如何根据邮政编码获取经度和纬度值
【问题讨论】:
-
尝试使用 google map api 进行地理编码
标签: ios iphone objective-c geocoding
我根据经度和纬度值获得了当前位置 然后我还使用注释在谷歌地图上获得了多个地方 现在我想根据邮政编码获取经度和纬度值 我不知道如何根据邮政编码获取经度和纬度值
【问题讨论】:
标签: ios iphone objective-c geocoding
这是调用google map api进行地理编码的方法,如果你通过Zipcode/Postal code,你会得到结果
-(void)ViewDidLoad
{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@",yourzipcodetext/PostalcodeText.text]]];
[request setHTTPMethod:@"POST"];
NSError *err;
NSURLResponse *response;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSString *resSrt = [[NSString alloc]initWithData:responseData encoding:NSASCIIStringEncoding];
NSLog(@"got response==%@", resSrt);
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];
NSString *lataddr=[[[[[dict objectForKey:@"results"] objectAtIndex:0]objectForKey:@"geometry"]objectForKey:@"location"]objectForKey:@"lat"];
NSString *longaddr=[[[[[dict objectForKey:@"results"] objectAtIndex:0]objectForKey:@"geometry"]objectForKey:@"location"]objectForKey:@"lng"];
NSLog(@"The resof latitude=%@", lataddr);
NSLog(@"The resof longitude=%@", longaddr);
}
Chioce-2
CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
[geoCoder geocodeAddressString:@"ZIP CODE HERE" completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
CLLocation *location = placemark.location;
CLLocationCoordinate2D coordinate = location.coordinate;
NSLog(@"Latitude %f", coordinate.latitude);
NSLog(@"Longitude %f", coordinate.longitude);
}];
【讨论】:
就像这样简单:
CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
[geoCoder geocodeAddressString:@"ZIP CODE HERE" completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
CLLocation *location = placemark.location;
CLLocationCoordinate2D coordinate = location.coordinate;
NSLog(@"Latitude %f", coordinate.latitude);
NSLog(@"Longitude %f", coordinate.longitude);
}];
【讨论】:
Apple 在forward geocoding 上发布了很好的文档,应该会对您有所帮助。
【讨论】: