ios – 从CLGeocoder获取当前城市和国家?

我一直在互联网上试图找出如何从CLGeocoder获得城市和国家.我可以很容易地获得经度和纬度,但是我需要城市和国家的信息,而且我仍然运用不客气的方法和任何想法?它基本上需要获取位置,然后为该国家提供Nsstring,并为该城市提供Nsstring,因此我可以使用它们查找更多信息或将其放在标签等上.

解决方法

您需要修改您的术语 – CLGeocoder(和大多数地理编码器)不会给您一个“城市”的一切 – 它使用诸如“管理区域”,“次级管理区域”等术语.CLGeocoder对象将返回一系列CLPlacemark对象,然后您可以查询所需的信息.您启动一个CLGeocoder并调用具有位置和完成块的reverseGeocodeLocation函数.以下是一个例子:
if (osversion() >= 5.0){

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

    [reverseGeocoder reverseGeocodeLocation:self.currentLocation completionHandler:^(NSArray *placemarks,NSError *error)
     {
         DDLogVerbose(@"reverseGeocodeLocation:completionHandler: Completion Handler called!");
         if (error){
             DDLogError(@"Geocode Failed with error: %@",error);
             return;
         }

         DDLogVerbose(@"Received placemarks: %@",placemarks);


         CLPlacemark *myPlacemark = [placemarks objectAtIndex:0];
         Nsstring *countryCode = myPlacemark.ISOcountryCode;
         Nsstring *countryName = myPlacemark.country;
         DDLogVerbose(@"My country code: %@ and countryName: %@",countryCode,countryName);

     }];
    }

现在请注意,CLPlacemark没有“city”属性.属性的完整列表可以在这里找到:CLPlacemark Class Reference

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...