Get the Zip Code of the Current Location - iPhone SDK

偶尔善良 提交于 2019-12-04 22:49:52

问题


How to get the zip code of the current location using mapkit, i didn't find any API's for getting this in document. i used coordinate,attitue,horizontal,vertical,course and speed parameters of CLLocationManager, but failed to get the zip code.

Could any one please give me the API or sample code to get it done.

Is it possible to get the zip code using current location in iphone?


回答1:


You can use the latitude and longitude to create an MKPlacemark object, which includes the zip code.

Here is a sample that shows how to do that.




回答2:


Reverse Geocoding in iPhone:

First add <MobileCoreServices/MobileCoreServices.h> framework.

-(void)CurrentLocationIdentifier
    {
        //---- For getting current gps location
        CLLocationManager *locationManager;
        CLLocation *currentLocation;

        locationManager = [CLLocationManager new];
        locationManager.delegate = self;
        locationManager.distanceFilter = kCLDistanceFilterNone;
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        [locationManager startUpdatingLocation];
    }

Reverse geocoding for getting place details using GPS location.

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    currentLocation = [locations objectAtIndex:0];
    [locationManager stopUpdatingLocation];

    CLGeocoder *geocoder = [[CLGeocoder alloc] init] ;
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error)
     {
         if (!(error))
         {
             CLPlacemark *placemark = [placemarks objectAtIndex:0];
            NSLog(@"\nCurrent Location Detected\n");
             NSLog(@"placemark %@",placemark);
             NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];

             NSString *Address = [[NSString alloc]initWithString:locatedAt];
             NSString *Zipcode = [[NSString alloc]initWithString:placemark.postalCode];
             NSLog(@"%@",Zipcode);      
         }
         else
         {
             NSLog(@"Geocode failed with error %@", error); // Error handling must required
         }
     }];
}

For more details to get from gps:

 placemark.region
 placemark.country
 placemark.locality
 placemark.name
 placemark.ocean
 placemark.postalCode
 placemark.subLocality
 placemark.location



回答3:


Swift version:

func getZipCode(location: CLLocation, completion: @escaping (String?) -> Void) {
    CLGeocoder().reverseGeocodeLocation(location) { placemarks, error in
        if let error = error {
            print("Failed getting zip code: \(error)")
            completion(nil)
        }
        if let postalCode = placemarks?.first?.postalCode {
            completion(postalCode)
        } else {
            print("Failed getting zip code from placemark(s): \(placemarks?.description ?? "nil")")
            completion(nil)
        }
    }
}


来源:https://stackoverflow.com/questions/5841152/get-the-zip-code-of-the-current-location-iphone-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!