简体   繁体   中英

Get coordinate from zip code (postal code) or city/state

Im using Map Kit and Core Location now and need to to get location information from zip code or city/state . Is there any way to do it?

You can use the CLGeocoder class which supports converting an address to a coordinate, and the reverse. For example:

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

There are a bunch of different methods you may want to use. You can limit the search to a particular region, for example.

You can use https://thezipcodes.com/ to get location from ZIP Code.

To integrate with your website follow http://thezipcodes.com/docs

Use this

-(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 method returns Coordinates using google api when you pass address as argument in this .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM