从 iOS 获取反向地理编码当前位置的最简单方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5058798/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:57:51  来源:igfitidea点击:

Easiest way of getting reverse geocoded current location from iOS

iosgeolocationreverse-geocoding

提问by kamziro

I saw from another question here : Determine iPhone user's countrythat it is possible to get the current country the user of the iPhone is in.

我从这里的另一个问题中看到:确定 iPhone 用户所在的国家/地区,可以获取iPhone 用户所在的当前国家/地区。

And that is quite convenient for many uses. However, would it be possible to go even deeper and infer from iOS (if it has the information) which state or city the user is in as well?

这对于许多用途来说非常方便。但是,是否可以更深入地从 iOS(如果有信息)推断用户所在的州或城市?

I suppose reverse geocoding services would be the next step if things weren't possible.. Are there even such things as a reverse geocoding service you can hire for your app though?

我想如果不可能的话,反向地理编码服务将是下一步。不过,是否有诸如反向地理编码服务之类的东西可以为您的应用程序租用?

采纳答案by Walter

I would start with the CLReverseGeocoderclass.

我会从CLReverseGeocoder课堂开始。

This stackoverflow questiongets the current city and can probably be adapted for your use.

这个 stackoverflow 问题获取当前城市,并且可能适合您的使用。

回答by fengd

MKReverseGeocoderis deprecated in iOS 5, now it's CLGeocoder

MKReverseGeocoder在 iOS 5 中已弃用,现在是 CLGeocoder

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
   [self.locationManager stopUpdatingLocation];

   CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
   [geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
       for (CLPlacemark * placemark in placemarks) {
           .... = [placemark locality];
        }    
    }];
}

回答by Ankit Vyas

CLGeocoder *geocoder = [[CLGeocoder alloc] init];

CLLocation *newLocation = [[CLLocation alloc]initWithLatitude:21.1700
                                                    longitude:72.8300];

[geocoder reverseGeocodeLocation:newLocation
               completionHandler:^(NSArray *placemarks, NSError *error) {

                   if (error) {
                       NSLog(@"Geocode failed with error: %@", error);
                       return;
                   }

                   if (placemarks && placemarks.count > 0)
                   {
                       CLPlacemark *placemark = placemarks[0];

                       NSDictionary *addressDictionary =
                       placemark.addressDictionary;

                       NSLog(@"%@ ", addressDictionary);
                       NSString *address = [addressDictionary
                                            objectForKey:(NSString *)kABPersonAddressStreetKey];
                       NSString *city = [addressDictionary
                                         objectForKey:(NSString *)kABPersonAddressCityKey];
                       NSString *state = [addressDictionary
                                          objectForKey:(NSString *)kABPersonAddressStateKey];
                       NSString *zip = [addressDictionary 
                                        objectForKey:(NSString *)kABPersonAddressZIPKey];


                       NSLog(@"%@ %@ %@ %@", address,city, state, zip);
                   }

               }];

Result

结果

{

  City = Surat;
  Country = India;
  CountryCode = IN;
  FormattedAddressLines =     (
    Surat,
    Gujarat,
    India
);
Name = Surat;
State = Gujarat;
} 
2012-12-20 21:33:26.284 CurAddress[4110:11603] (null) Surat Gujarat (null)

回答by Yi Jiang

Following codes can be easy to retrieve full-details.

以下代码可以轻松检索完整详细信息。

 [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        if(placemarks.count){

        placeNameLabel.text = [placemarks[0] name];
        streetNumberLabel.text = [placemarks[0] subThoroughfare];
        streetLabel.text = [placemarks[0] thoroughfare];
        neighborhoodLabel.text = [placemarks[0] subLocality];
        cityLabel.text = [placemarks[0] locality];
        countyLabel.text = [placemarks[0] subAdministrativeArea];
        stateLabel.text = [placemarks[0] administrativeArea];    //or province 
        zipCodeLabel.text = [placemarks[0] postalCode];
        countryLabel.text = [placemarks[0] country];
        countryCodeLabel.text = [placemarks[0] ISOcountryCode];
        inlandWaterLabel.text = [placemarks[0] inlandWater];
        oceanLabel.text = [placemarks[0] ocean];
        areasOfInterestLabel.text = [placemarks[0] areasOfInterest[0]];
        }
    }];