使用IOS中的Geocoder类获取基于zip的纬度和经度

我得到了当前位置基于经度和纬度值,然后我也得到了多个地方在谷歌地图上使用Annotation现在我想得到经度和纬度值的基础上邮编我不知道如何获取经度和纬度值基于Zip码

这是调用谷歌地图API的地理编码的方法,如果你通过邮政编码/邮政编码,你会得到结果

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

苹果公司发布了关于前向地理编码的良好文档,可以帮助你。