【问题标题】:Easiest way of getting reverse geocoded current location from iOS从 iOS 获取反向地理编码当前位置的最简单方法
【发布时间】:2011-06-30 20:04:38
【问题描述】:

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

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

如果不可能的话,我认为反向地理编码服务将是下一步。但是,您是否可以为您的应用雇用反向地理编码服务?

【问题讨论】:

    标签: ios geolocation reverse-geocoding


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:
      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)
      

      【讨论】:

      • #import 你也必须添加这个框架。
      【解决方案3】:

      我将从CLReverseGeocoder 类开始。

      This stackoverflow question 获取当前城市并且可能适合您使用。

      【讨论】:

      • 此方法在 iOS 5 中已弃用。
      【解决方案4】:

      以下代码可以很容易地检索到完整的详细信息。

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

      【讨论】:

      • “.subThoroughfare”的原始值将包含一个破折号,即 \U2013,您必须处理这个以便向用户显示一个用户友好的地址。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      相关资源
      最近更新 更多