本文介绍了使用IOS中的Geocoder类基于zip获取经度和纬度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我根据经度和纬度值获得了当前位置
然后我还使用Annotation
获得谷歌地图上的多个位置现在我想根据邮政编码获得经度和纬度值
我不知道如何根据邮政编码得到经度和纬度值

I got the current location based on Longitude and latitude values and then I also got Multiple places on google map using Annotation now I want get longitude and latitude values based on Zip code I don't know how to get Longitude and latitude values base on Zip code

推荐答案

这是调用google map api的方法对于地理编码,如果您通过邮政编码/邮政编码,则会得到结果

this is the method for call google map api for geocoding, if u pass the Zipcode/Postal code, you get the result

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

这篇关于使用IOS中的Geocoder类基于zip获取经度和纬度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 11:53