【问题标题】:Get coordinate from zip code (postal code) or city/state从邮政编码(邮政编码)或城市/州获取坐标
【发布时间】:2013-04-05 00:16:10
【问题描述】:

我现在正在使用 Map Kit 和 Core Location,需要从 zip codecity/state 获取 location 信息。有什么办法吗?

【问题讨论】:

标签: iphone ios objective-c ios5 mapkit


【解决方案1】:

您可以使用CLGeocoder 类,它支持将地址转换为坐标,反之亦然。例如:

[geocoder geocodeAddressString:@"<postcode here>"
             completionHandler:^(NSArray* placemarks, NSError* error){
                 for (CLPlacemark* aPlacemark in placemarks)
                 {
                     // Process the placemark.
                 }
}];

您可能想要使用许多不同的方法。例如,您可以将搜索限制在特定区域。

【讨论】:

  • 嗯,不知道可以这样用。非常感谢
【解决方案2】:

您可以使用https://thezipcodes.com/ 从邮政编码获取位置。

要与您的网站集成,请关注http://thezipcodes.com/docs

【讨论】:

    【解决方案3】:

    使用这个

    -(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 返回 坐标

    【讨论】:

    • 该解决方案的限制为每天 2500 个请求,而且结果必须显示在 goolge 地图上,因此不适用于 iOS6。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    • 2014-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多