iphone - CLLocationManager 没有获取城市名称

标签 iphone ios objective-c cocoa-touch cllocationmanager

我正在尝试通过以下代码使用 CLLocationManager 获取当前城市和国家/地区 -

#pragma mark - Core Location Delegate Methods
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    CLGeocoder *reverseGeocoder = [[CLGeocoder alloc] init];

    [reverseGeocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error)
     {
         NSLog(@"reverseGeocodeLocation:completionHandler: Completion Handler called!");
         if (error){
             NSLog(@"Geocode failed with error: %@", error);
             return;
         }

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


         CLPlacemark *myPlacemark = [placemarks objectAtIndex:0];
         NSString *countryCode = myPlacemark.ISOcountryCode;
         NSString *countryName = myPlacemark.country;
         NSString *city1 = myPlacemark.subLocality;
         NSString *city2 = myPlacemark.locality;
         NSLog(@"My country code: %@, countryName: %@, city1: %@, city2: %@", countryCode, countryName, city1, city2);
     }];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
    CLLocationDirection th=[newHeading trueHeading];
    NSLog(@"True Heading value is=%f",th);
    CLLocationDirection magnetic=[newHeading magneticHeading];
    NSLog(@"Magnetic Heading value is=%f",magnetic);
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSString *errorType = (error.code == kCLErrorDenied) ? NSLocalizedString(@"access_denied", @"") : NSLocalizedString(@"unknown_error", @"");
    UIAlertView *alert = [[UIAlertView alloc]
                      initWithTitle:NSLocalizedString(@"error_getting_location", @"")
                      message:errorType
                      delegate:nil
                      cancelButtonTitle:NSLocalizedString(@"ok", @"")
                      otherButtonTitles:nil];
    [alert show];
}

它总是给出结果 -

My country code: IN, countryName: India, city1: (null), city2: (null)

我不知道这可能是什么问题。有没有人遇到过无法使用 CLLocationManager

获取城市名称的问题

最佳答案

已编辑:

- (void) getReverseGeocode
{
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];

    if(currentLatLong.count > 0)
    {
        CLLocationCoordinate2D myCoOrdinate;

        myCoOrdinate.latitude = LatValue;
        myCoOrdinate.longitude = LangValue;

        CLLocation *location = [[CLLocation alloc] initWithLatitude:myCoOrdinate.latitude longitude:myCoOrdinate.longitude];
        [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error)
         {
             if (error)
             {
                 NSLog(@"failed with error: %@", error);
                 return;
             }
             if(placemarks.count > 0)
             {
                 NSString *MyAddress = @"";
                 NSString *city = @"";

                 if([placemark.addressDictionary objectForKey:@"FormattedAddressLines"] != NULL)
                     MyAddress = [[placemark.addressDictionary objectForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
                 else
                     MyAddress = @"Address Not founded";

                 if([placemark.addressDictionary objectForKey:@"SubAdministrativeArea"] != NULL)
                     city = [placemark.addressDictionary objectForKey:@"SubAdministrativeArea"];
                 else if([placemark.addressDictionary objectForKey:@"City"] != NULL)
                     city = [placemark.addressDictionary objectForKey:@"City"];
                 else if([placemark.addressDictionary objectForKey:@"Country"] != NULL)
                     city = [placemark.addressDictionary objectForKey:@"Country"];
                 else
                     city = @"City Not founded";

               NSLog(@"%@",city);
               NSLog(@"%@", MyAddress);
             }
         }];
    }
}

关于iphone - CLLocationManager 没有获取城市名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15270527/

相关文章:

ios - 获取从 UIImagePickerController 选择的原始视频名称

ios - UITableView 显示循环的最后一个值

ios - UICollection 的镜像滚动

ios - 将按钮导出添加到 IBoutletCollection Xcode

objective-c - NSRunAlertPanel 是否在任何空间上显示警报? - Objective-C/10.7+

iphone - App Store iphone应用程序版本说明。如何设置?

ios - @try-@finally 保证在处理 ARC 未跟踪的对象时消除内存泄漏

iphone - UITextView 子类中的 viewDidLoad?

ios - 如何在保留通知中心的同时清除角标(Badge)编号

objective-c - 如何在Cocoa中的App包中包含框架安装包?