CLGeocoder returns null results for city and state when using current location data

南楼画角 提交于 2019-12-13 01:18:14

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/21194958/clgeocoder-returns-null-results-for-city-and-state-when-using-current-location-d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!