ios 获取当前位置的邮政编码 - iPhone SDK
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5841152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the Zip Code of the Current Location - iPhone SDK
提问by Shiva Reddy
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.
如何使用 mapkit 获取当前位置的邮政编码,我没有在文档中找到任何用于获取此代码的 API。我使用了 CLLocationManager 的坐标、姿态、水平、垂直、路线和速度参数,但无法获取邮政编码。
Could any one please give me the API or sample code to get it done.
任何人都可以给我 API 或示例代码来完成它。
Is it possible to get the zip code using current location in iphone?
是否可以使用 iPhone 中的当前位置获取邮政编码?
采纳答案by Nick
You can use the latitude and longitude to create an MKPlacemark object, which includes the zip code.
您可以使用纬度和经度创建一个MKPlacemark 对象,其中包括邮政编码。
Hereis a sample that shows how to do that.
这是一个示例,展示了如何做到这一点。
回答by Rajesh Loganathan
Reverse Geocoding in iPhone:
iPhone 中的反向地理编码:
First add <MobileCoreServices/MobileCoreServices.h>
framework.
首先添加<MobileCoreServices/MobileCoreServices.h>
框架。
-(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.
使用 GPS 位置获取地点详细信息的反向地理编码。
- (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:
欲了解更多详情,请从 GPS 获取:
placemark.region
placemark.country
placemark.locality
placemark.name
placemark.ocean
placemark.postalCode
placemark.subLocality
placemark.location
回答by superarts.org
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)
}
}
}