oOOMIAO 2013-03-14 03:07 采纳率: 10%
浏览 4384
已采纳

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)

请高手指点怎么修改。不胜感激。

  • 写回答

1条回答

  • Chengzi_963 2013-03-14 06:06
    关注
    - (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);
                 }
             }];
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)