2 ooomiao oOOMIAO 于 2013.03.14 11:07 提问

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
Chengzi_963   2013.03.14 14: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);
             }
         }];
    }
}
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!
其他相关推荐
根据经纬度获取城市名
这是我自己的笔记,如有侵犯到您请留言联系 private void initGecooder() { Geocoder gc = new Geocoder(this, Locale.getDefault()); List locationList = null; try { locationList = gc.getFromLocation(strlat, strlon, 1); }
根据城市名字获取经纬度
直接上代码:<?php function addresstolatlag($address) { $url = 'http://api.map.baidu.com/geocoder/v2/?address=' . $address . '&output=json&ak=xxx'; if ($result = file_get_contents($url)) { ret
通过IP获取城市名称
自己测试过,和大家分享一下
Android中通过经纬度来过去到城市名称
Android中通过经纬度来过去到城市名称Android中通过经纬度来过去到城市名称
百度地图SDK 网络定位城市名称
温馨提示:1.我们要申请百度地图定位API帐号:http://lbsyun.baidu.com/index.php?title=android-locsdk/guide/key                     2.获取百度的Key,获取密钥有详细步骤,那我就不一一说了。                     3.下载百度类包:http://lbsyun.baidu.c
如何利用百度地区定位到城市名
test              //百度地图API功能 var map = new BMap.Map('map'); //获得当前城市 var myCity = new BMap.LocalCity(); myCity.get(myFun); function myFun(result){     va
只获取城市名称定位(简单)
先导包和配置环境,权限 根据百度地图开发中心一步步来就行 package com.zixue.baiduditu;   import com.baidu.location.BDLocation; import com.baidu.location.BDLocationListener; import com.baidu.location.Location
系统自带定位坐标转为城市名
//系统自带定位 [[MPLocationManager shareInstance] startSystemLocationWithRes:^(CLLocation *loction, NSError *error) { if (!error) { CLGeocoder *geocoder=[[CLGeocoder alloc]init];
Google地图获取城市名称
使用Google Map获取城市名称通过Gps或是NetWork获取到经纬度,在通过百度或是谷歌地图来实现经纬度到城市具体地址的编码。 获取经纬度:public static Location getLocation(Context context){ Location location=null; LocationManager manager = (LocationManage
iOS定位获取城市名
在项目中经常需要获取用户的位置信息,这就涉及到了定位,这里举个简单的例子——定位获取城市名,来归纳coreLocation的一些基本用法。 1.在iOS中如果需要使用定位服务,需要导入定位的框架 定位主要用到一个CLLocationManager对象及它的代理方法,所以在需要的地方设置一个manager对象,并设置其代理,要继承CLLocationManagerDelegate协议.实现代理方法。