【问题标题】:Reverse Geocoding issues for City name in iOSiOS中城市名称的反向地理编码问题
【发布时间】:2012-06-25 04:36:52
【问题描述】:

我可以使用,在我的 iPad 应用程序中检索当前位置,

CLLocation *location = [[CLLocation alloc] initWithLatitude:[latitude floatValue]   longitude:[longitude floatValue]];
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];

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

         NSLog(@"-----------------Placemark is %@-----------------", placemarks);

          locationLabel.text = placemarks;

     }];

输出是,

-----------------Placemark is ("South Atlantic Ocean, South Atlantic Ocean @<-42.60533670,-21.93128480> +/- 100.00m,    region (identifier <-41.51023865,-31.60774370> radius 4954476.31) <-41.51023865,-31.60774370> radius 4954476.31m"
)-----------------

我可以使用相同的信息来获取城市和国家名称吗?而不是一长串信息?

此外,'locationLabel.text = placemarks' 会发出警告,“从 'NSArray*_strong' 分配给 'NSString*' 的指针类型不兼容,我无法解决。

【问题讨论】:

    标签: ipad reverse-geocoding cllocation clgeocoder


    【解决方案1】:

    是的,你可以。
    但是你做的有点不对。首先,placemarks 是一个数组而不是字符串。这就是locationLabel.text = placemarks 发出警告的原因。

    PlacemarksCLPlacemarks 的数组。这是因为地理编码器可以为一个坐标返回多个结果。在最简单的情况下,其中的第一项应该没问题。

    CLPlacemark 具有属性 addressDictionary,其中包含此位置的数据。 您可以使用ABPerson 头文件定义的地址属性常量访问此数据。

    例如:

    从数组中获取第一个地标:

    CLPlacemark *place = [placemarks objectAtIndex:0];
    

    然后从这个地标获取城市:

    NSString *cityName = [place objectForKey: kABPersonAddressCityKey];
    

    别忘了导入 AVPerson 标头!

    【讨论】:

    • 谢谢yinkou,发帖后我才意识到自己的错误!为什么加载位置需要一些时间,比如一两秒?
    【解决方案2】:

    您可以获得以下所有地点的详细信息

            placeNameLabel.text     = [placemarks[0] name];
            addressNumberLabel.text = [placemarks[0] subThoroughfare];
            addressLabel.text       = [placemarks[0] thoroughfare];
            neighborhoodLabel.text  = [placemarks[0] subLocality];
            cityLabel.text          = [placemarks[0] locality];
            countyLabel.text        = [placemarks[0] subAdministrativeArea];
            stateLabel.text         = [placemarks[0] administrativeArea];
            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];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-10
      • 2011-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多