从CLGeocoder获取当前的城市和国家? [英] Get current city and country from CLGeocoder?

查看:482
本文介绍了从CLGeocoder获取当前的城市和国家?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在互联网上试图了解如何从 CLGeocoder 获取城市和国家。我可以轻松获得经度和纬度,但我需要城市和国家的信息,我一直在使用已弃用的方法等等,任何想法?它基本上需要获取该位置,然后为该国家/地区设置 NSString ,为该城市设置 NSString ,我可以使用它们查找更多信息或将它们放在标签等上。

I've been all over the internet trying to find out how to get the city and country from CLGeocoder. I can get the longitude and latitude easily but I need the city and country information, and I keep running into deprecated methods and such, any ideas? It basically needs to get the location, then have an NSString for the country, and an NSString for the city, so I can use them to look up more info or put them on labels, etc.

推荐答案

您需要稍微修改一下术语 - CLGeocoder(和大多数地理编码器)本身不会给你一个城市 - 它使用诸如管理区域,子管理区域等术语.CLGeocoder对象将返回一个CLPlacemark对象数组,你可以然后查询您需要的信息。您初始化CLGeocoder并使用位置和完成块调用reverseGeocodeLocation函数。这是一个例子:

You need to revise your terminology a bit - CLGeocoder (and most geocoders) won't give you a 'city' per-se - it uses terms such as 'Administrative Area', 'Subadministrative Area', etc. The CLGeocoder object will return an array of CLPlacemark objects which you can then query for the information you need. You init a CLGeocoder and call the reverseGeocodeLocation function with a location and a completion block. Here's an example:

    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类参考

Now note that the CLPlacemark doesn't have a 'city' property. The full list of properties can be found here: CLPlacemark Class Reference

这篇关于从CLGeocoder获取当前的城市和国家?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆