【问题标题】:Get latitude and longitude based on zip using Geocoder class in IOSIOS中使用Geocoder类根据zip获取经纬度
【发布时间】:2014-03-02 23:07:32
【问题描述】:

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

【问题讨论】:

  • 尝试使用 google map api 进行地理编码

标签: ios iphone objective-c geocoding


【解决方案1】:

这是调用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);
    }];

【讨论】:

  • 这是你的邮政编码/邮政编码文本或字符串,你传递值然后你得到响应,你明白吗
  • 感谢重播我会尝试
  • NSString *Zipcodetext;然后设置 Zipcodetext.text 但它是错误的(在对象 NSString 上找不到属性文本),然后如何传递我的邮政编码,如 534401
  • gt 创建 NSString *zipcode=534401; k 并将 Zipcode 替换为 URL 上的 Zipcodetext.text,k
  • 谢谢兄弟我得到了输出 thk bro
【解决方案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);
    }];

【讨论】:

  • 感谢重播,我得到了纬度和经度,但它显示的位置不同,请帮助我提前了解
【解决方案3】:

Apple 在forward geocoding 上发布了很好的文档,应该会对您有所帮助。

【讨论】:

    猜你喜欢
    • 2014-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多