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

时间:2011-02-20 17:47:10

标签: ios geolocation reverse-geocoding

我从另一个问题中看到:Determine iPhone user's country可以获得iPhone用户所在的当前国家/地区。

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

如果事情不可能,我认为反向地理编码服务将成为下一步。有没有可以为您的应用聘用的反向地理编码服务?

4 个答案:

答案 0 :(得分:26)

在iOS 5中不推荐使用

MKReverseGeocoder,现在它是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];
        }    
    }];
}

答案 1 :(得分:24)

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

               }];

<强>结果

{

  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)

答案 2 :(得分:5)

我会从CLReverseGeocoder课开始。

This stackoverflow question获取当前城市,可能会根据您的需要进行调整。

答案 3 :(得分:3)

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

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