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

时间:2013-01-29 05:32:03

标签: ios ios6 nsstring clgeocoder currentlocation

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

2 个答案:

答案 0 :(得分:18)

您需要稍微修改一下术语 - CLGeocoder(以及大多数地理编码器)不会给您一个“城市”本身 - 它使用诸如“管理区域”,“子管理区域”等术语.CLGeocoder object将返回一个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

答案 1 :(得分:0)

您可以使用以下代码获取城市,国家/地区和iso国家(Swift 5):

$ch = curl_init('https://api.companieshouse.gov.uk/company/<companynumber>');
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
        curl_setopt($ch, CURLOPT_POSTREDIR, 3);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow http 3xx redirects
        curl_setopt($ch, CURLOPT_USERPWD, "AUTH_KEY" . ":" . "");
        $resp_orders = curl_exec($ch); // execute

        print_r($resp_orders['company_name']);
相关问题