地标未在iOS 6中提供城市名称

时间:2012-11-05 16:57:55

标签: iphone objective-c ios6 cllocation clgeocoder

我正在使用此代码,我正在获取地标,但它没有给出城市名称。 之前我使用MKReverse Geocoder来获取我获得城市名称的地标,但是在iOS 6中它显示已被弃用,因为Apple开发人员在CLLocation中添加了所有内容。

所以我使用了这段代码:

-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{

    CLLocation *location = [locationManager location];
    NSLog(@"location is %@",location);

    CLGeocoder *fgeo = [[[CLGeocoder alloc] init] autorelease];

    // Reverse Geocode a CLLocation to a CLPlacemark
    [fgeo reverseGeocodeLocation:location
          completionHandler:^(NSArray *placemarks, NSError *error){

               // Make sure the geocoder did not produce an error
               // before continuing
               if(!error){
                    // Iterate through all of the placemarks returned
                    // and output them to the console
                    for(CLPlacemark *placemark in placemarks){
                           NSLog(@"%@",[placemark description]);
                           city1= [placemark.addressDictionary objectForKey:(NSString*) kABPersonAddressCityKey];
                           NSLog(@"city is %@",city1);
                    }
               }
               else{
                    // Our geocoder had an error, output a message
                    // to the console
                    NSLog(@"There was a reverse geocoding error\n%@",
                                         [error localizedDescription]);
               }
         }
    ];

}

正如我在NSLog(@"%@",[placemark description]);的控制台中看到的那样 它提供的输出如下: - abc道路名称,abc道路名称,州名称,国家名称。

2 个答案:

答案 0 :(得分:4)

如果你想要NSLog的地址,你必须做这样的事情:

NSString *street = [[placemark addressDictionary] objectForKey:(NSString *)kABPersonAddressStreetKey];
NSString *city = [[placemark addressDictionary] objectForKey:(NSString *)kABPersonAddressCityKey];
NSString *state = [[placemark addressDictionary] objectForKey:(NSString *)kABPersonAddressStateKey];
NSString *country = [[placemark addressDictionary] objectForKey:(NSString *)kABPersonAddressCountryKey];
NSString *zip = [[placemark addressDictionary] objectForKey:(NSString *)kABPersonAddressZIPKey];

NSString *message = [NSString stringWithFormat:@"Address Is: %@, %@ %@, %@, %@", street, zip, city, state, country];

NSLog(@"%@", message);

或者您可以遍历返回的数组:

NSArray *array = [[placemark addressDictionary] objectForKey:@"FormattedAddressLines"];

如果您只想打印字典内容,请执行以下操作:

NSLog(@"%@", [[placemark addressDictionary] description]);

答案 1 :(得分:4)

我能看到的三件事可以引起注意......

首先,不推荐使用此方法(see documentation here):

(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

请尝试使用此代码:

(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations

您最新的位置是数组locations中的最后一项:

 /* 
 According to the Apple docs, the array of locations keeps the most recent location as the
 last object in the array 
 */
CLLocation *location = [locations lastObject];
NSLog(@"%@", location);

其次,如果您使用ARC,则不需要自动发布消息:CLGeocoder *fgeo = [[CLGeocoder alloc] init];正常工作。

最后,根据CLPlacemark的Apple文档,有一个属性locality,它应该返回该地标的城市。所以

for(CLPlacemark *placemark in placemarks){
    NSLog(@"%@",[placemark description]);
    NSString *city1 = [placemark locality];
    NSLog(@"city is %@",city1); }

如果你只是想要这个城市,那么addressDictionary属性似乎有点矫枉过正。根据{{​​3}}地址字典被格式化为返回ABPerson对象中的东西,我假设您必须解析才能获得该城市。地标位置似乎更简单......

我在我正在构建的地理编码应用中测试了我的建议,并通过[placemark locality]

获得了您正在寻找的结果