1

我正在MKReverseGeocoderlocationManager's didUpdateToLocation委托一起使用但我想知道当我在Device.

我的代码是波纹管: -

- (void)viewDidLoad 
{
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
    [locationManager startUpdatingLocation];


     [super viewDidLoad];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    // this creates a MKReverseGeocoder to find a placemark using the found coordinates
    reverseGeocoder = [[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate];
    reverseGeocoder.delegate = self;
    [reverseGeocoder start];
}

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error
{
    NSLog(@"MKReverseGeocoder has failed.");
}

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
    MKPlacemark * myPlacemark = placemark;
    NSString *city = myPlacemark.thoroughfare;
    NSString *subThrough=myPlacemark.subThoroughfare;
    NSString *locality=myPlacemark.locality;
    NSString *subLocality=myPlacemark.subLocality;
    NSString *adminisArea=myPlacemark.administrativeArea;
    NSString *subAdminArea=myPlacemark.subAdministrativeArea;
    NSString *postalCode=myPlacemark.postalCode;
    NSString *country=myPlacemark.country;
    NSString *countryCode=myPlacemark.countryCode;


    NSLog(@"city%@",city);
    NSLog(@"subThrough%@",subThrough);
    NSLog(@"locality%@",locality);
    NSLog(@"subLocality%@",subLocality);
    NSLog(@"adminisArea%@",adminisArea);
    NSLog(@"subAdminArea%@",subAdminArea);
    NSLog(@"postalCode%@",postalCode);
    NSLog(@"country%@",country);
    NSLog(@"countryCode%@",countryCode);

}

输出

cityDrive-in Road

subThrough(null)

locality(null)

subLocality(null)

adminisAreaGujarat

subAdminArea(null)

postalCode(null)

countryIndia

countryCodeIN

使用 CLGeocoder

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    // this creates a MKReverseGeocoder to find a placemark using the found coordinates
//    reverseGeocoder = [[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate];
//    reverseGeocoder.delegate = self;
//    [reverseGeocoder start];

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

    [reverseGeo reverseGeocodeLocation: newLocation completionHandler:
     ^(NSArray *placemarks, NSError *error) {

         NSLog(@"%@",[placemarks objectAtIndex:0]);
         for (CLPlacemark *placemark in placemarks) {



             NSLog(@"~~~~~~~~%@",placemark.locality);

         }


     }];
}

OUTPUT IS:- ~~~~~~~~(null)

我上面的代码在模拟器中正常工作,但在设备中不能正常工作.. :(请帮助并指导我正确获取城市名称的方法

4

1 回答 1

2

您不应该真正使用 MKReverseGeocoder,因为它在 iOS5 中已被苹果贬值,您应该真正使用 CLGeocoder。CLGeocode 将返回基于 NSArray *placemarks 的信息,然后您可以遍历它们并调用 assoc 数组中的键。请通过链接。

CLGeoCode 类参考

相关示例代码

于 2012-12-05T13:05:52.693 回答