获取当前位置的邮政编码 - iPhone SDK

发布于 2024-11-04 02:50:39 字数 172 浏览 3 评论 0原文

如何使用mapkit获取当前位置的邮政编码,我在文档中没有找到任何用于获取此信息的API。我使用了CLLocationManager的坐标、姿态、水平、垂直、航向和速度参数,但未能获取邮政编码。

任何人都可以给我 API 或示例代码来完成它。

是否可以使用 iPhone 中的当前位置获取邮政编码?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

策马西风 2024-11-11 02:50:40

iPhone 中的反向地理编码:

首先添加 框架。

-(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];
    }

使用 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
         }
     }];
}

如需从 GPS 获取更多详细信息:

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

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
紫竹語嫣☆ 2024-11-11 02:50:40

您可以使用纬度和经度创建 MKPlacemark 对象,其中包含邮政编码。

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

┊风居住的梦幻卍 2024-11-11 02:50:40

斯威夫特版本:

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)
        }
    }
}

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)
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文