CLGeocoder returns null results for city and state

2019-07-23 12:35发布

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条回答
一纸荒年 Trace。
2楼-- · 2019-07-23 13:25

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.

查看更多
登录 后发表回答