本文介绍了使用当前位置数据时,CLGeocoder返回城市和州的空结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用CLGeocoder获取城市和州的名称。但是,placemark.addressDictionary返回了我:



{
FormattedAddressLines =(
南大西洋
);
名称=南大西洋;
Ocean =南大西洋;
}



,地标是:
南大西洋,南大西洋@ --- 42.60533670,-21.93128480> +/- 100.00m,区域CLCircularRegion(标识符:'半径4958095.65',中心:,半径:4958095.65m)



Also ,[地标位置]的NSLog和[地标管理区域]都显示为nil。



还尝试添加ABAdressBookUI,ABAdressBook框架,并将数据获取为:



NSString * addressDict = ABCreateStringWithAddressDictionary(placemark.addressDictionary,NO);



或尝试过:
NSString * state =(NSString *)[placemark.addressDictionary objectForKey:kABPersonAddressStateKey];



如果有任何可能的解决方案,或者如果我缺少某些内容,请告诉我。



点击当前位置按钮时的代码如下:



CLLocation * loc;

  loc = [[CLLocation alloc] initWithLatitude:[KLocationManager sharedManager] ._ locationManager.location.coordinate.latitude经度:[KLocationManager sharedManager] ._ locationManager.location.coordinate.longitude]; 

CLGeocoder * geocoder = [[CLGeocoder alloc] init];

[geocoder reverseGeocodeLocation:loccompleteHandler:^(NSArray * placemarks,NSError * error){
CLPlacemark * placemark = [placemarks objectAtIndex:0];
if(placemark){
NSDictionary * dic = placemark.addressDictionary;
NSString * locName = [NSString stringWithFormat:@%@,%@,[dic objectForKey:@ City],[dic objectForKey:@ State]];
CLLocation * loc = placemark.location;

self.returnLocationDict = @ {@ name:locName,@ location:loc};
}
}];

我在这里想念什么?

解决方案

复制您正在体验的一种方法是使用纬度和经度为0,0的CLLocation



我运行了您的

您的位置管理器可能未返回您期望的结果。

使用纬度:37.3318经度:-122.0312(无限循环..) p>

Hi I am trying to get the name of city and state using CLGeocoder. However, placemark.addressDictionary is returning me :

{ FormattedAddressLines = ( "South Atlantic Ocean" ); Name = "South Atlantic Ocean"; Ocean = "South Atlantic Ocean";}

and placemark is:South Atlantic Ocean, South Atlantic Ocean @ <-42.60533670,-21.93128480> +/- 100.00m, region CLCircularRegion (identifier:'<-41.51023865,-31.60774370> radius 4958095.65', center:<-41.51023865,-31.60774370>, radius:4958095.65m)

Also, NSLog of [placemark locality] and [placemark administrativeArea] shows both nil.

Also tried adding ABAdressBookUI , ABAdressBook framework and get the data as:

NSString *addressDict = ABCreateStringWithAddressDictionary(placemark.addressDictionary, NO); which returns an object with empty description.

or tried:NSString *state = (NSString *)[placemark.addressDictionary objectForKey:kABPersonAddressStateKey]; which throws warning of incompatible pointer types.

If there is any possible solution or if I am missing something please let me know.

My code when current location button is tapped is as follows:

CLLocation *loc;

loc = [[CLLocation alloc] initWithLatitude:[KLocationManager sharedManager]._locationManager.location.coordinate.latitude    longitude:[KLocationManager sharedManager]._locationManager.location.coordinate.longitude];

CLGeocoder* geocoder = [[CLGeocoder alloc] init];

[geocoder reverseGeocodeLocation:loc completionHandler:^(NSArray *placemarks, NSError *error) {
    CLPlacemark* placemark = [placemarks objectAtIndex:0];
    if (placemark) {
        NSDictionary* dic = placemark.addressDictionary;
        NSString* locName = [NSString stringWithFormat:@"%@, %@", [dic objectForKey:@"City"],[dic objectForKey:@"State"]];
        CLLocation* loc = placemark.location;

        self.returnLocationDict = @{@"name":locName, @"location":loc};
    }
}];

What am I missing here?

解决方案

One way of replicating what you're experiencing is to use a CLLocation with latitude and longitude of 0,0

I ran your code by using latitude:37.3318 longitude:-122.0312 (Infinite Loop..) and it worked as expected.

Your location manager is probably not returning what you expect.

这篇关于使用当前位置数据时,CLGeocoder返回城市和州的空结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 19:15