【问题标题】:CLGeocoder and backward compatibilityCLGeocoder 和向后兼容性
【发布时间】:2023-04-03 03:45:01
【问题描述】:

我有一个简单的问题。

MKReverseCoder 已被弃用,并且自 iOS 5 起无法使用。我们必须使用CLGeocoder。但是,iOS4下的人很多。新应用如何与 iOS4 和 iOS5 一起使用进行地理编码?

谢谢!

【问题讨论】:

    标签: iphone ios5 backwards-compatibility reverse-geocoding


    【解决方案1】:
    - (IBAction)geoCodeLocation:(id)sender{
        [self.geoCoder reverseGeocodeLocation: locationManager.location completionHandler: 
         ^(NSArray *placemarks, NSError *error) {
             CLPlacemark *placemark = [placemarks objectAtIndex:0];
             NSLog(@"placemark %@",placemark);
             //String to hold address
             NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
             NSLog(@"addressDictionary %@", placemark.addressDictionary);
    
             NSLog(@"placemark %@",placemark.region);
             NSLog(@"placemark %@",placemark.country);  // Give Country Name 
             NSLog(@"placemark %@",placemark.locality); // Extract the city name 
             NSLog(@"location %@",placemark.name);
             NSLog(@"location %@",placemark.ocean);
             NSLog(@"location %@",placemark.postalCode);
             NSLog(@"location %@",placemark.subLocality);
    
             NSLog(@"location %@",placemark.location);
              //Print the location to console
             NSLog(@"I am currently at %@",locatedAt);
    
             //Set the label text to current location
             [locationLabel setText:locatedAt];
    
         }];
    

    更多请看property of CLPlacemark

    【讨论】:

      【解决方案2】:

      如果有人试图从 MKReverseGeocoder 迁移到 CLGeocoder,那么我写了一篇博文,可能会有所帮助 http://jonathanfield.me/jons-blog/clgeocoder-example.html

      基本上一个例子是,在您创建 locationManager 和 CLGeocoder 对象后,只需将此代码添加到您的 viewDidLoad(),然后制作一些标签或文本区域来显示数据。

      [super viewDidLoad]; locationManager.delegate = self; [locationManager startUpdatingLocation];
      
      locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
      [self.CLGeocoder reverseGeocodeLocation: locationManager.location completionHandler: 
       ^(NSArray *placemarks, NSError *error) {
      
           CLPlacemark *placemark = [placemarks objectAtIndex:0];
      
               isoCountryCode.text = placemark.ISOcountryCode;
               country.text = placemark.country;
               postalCode.text= placemark.postalCode;
               adminArea.text=placemark.administrativeArea;
               subAdminArea.text=placemark.subAdministrativeArea;
               locality.text=placemark.locality;
               subLocality.text=placemark.subLocality;
               thoroughfare.text=placemark.thoroughfare;
               subThoroughfare.text=placemark.subThoroughfare;
               //region.text=placemark.region;
      
      }];
      

      【讨论】:

      • 如何初始化 CLGeoCoder 对象?
      • 这里没有给出完整的邮政编码!它给出了类似“HP2 7”的内容,最后两个字符丢失了。你知道可能是什么原因吗?如果我查询 Google API - maps.googleapis.com/maps/api/geocode/…,它会返回完整的邮政编码!
      【解决方案3】:

      MKReverseGeocoder 仍然适用于 iOS5。它只是被弃用了,这意味着它将在稍后的某个时间点被删除(比如在 iOS6 之类的发布时)。所以你现在可以继续使用它,没有任何问题

      【讨论】:

      • 我收到如下消息:错误域=NSURLErrorDomain 代码=-1011“操作无法完成。(NSURLErrorDomain 错误 -1011。)”。而且它之前工作过......
      • 这绝对是奇怪的。我相信这是一个身份验证错误,但找到它的来源将是一个问题。似乎 MKReverseGeocoder 仍然适用于我的应用程序(为 iOS4.3 构建,但在 iOS5 设备上运行),所以我觉得错误来自其他地方(或者可能是谷歌服务器的临时问题)
      • 好的。现在它起作用了。那只是暂时的,这与我升级 iOS5 的巧合非常糟糕……谢谢您的帮助!
      • 很高兴,我很高兴听到它现在正在工作。但是,是的,不要太担心被贬低的方法。它们会在实际被删除之前工作很长一段时间,让您和您的用户有足够的时间切换到新方法和新操作系统
      猜你喜欢
      • 2011-02-06
      • 1970-01-01
      • 1970-01-01
      • 2012-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多