我正在MKReverseGeocoder
使用locationManager's
didUpdateToLocation
委托但我想知道在Device
测试应用时,某些值会变为空。
我的代码是Bellow: -
- (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)
我的上述代码在模拟器中正常工作但在设备中没有... :(请帮助并指导我以正确的方式获取城市名称
答案 0 :(得分:2)
你不应该真的使用MKReverseGeocoder,因为在iOS5中苹果已经折旧了,你应该真的使用CLGeocoder。 CLGeocode将返回一个基于NSArray *地标的信息,然后您可以遍历它们并调用assoc数组中的键。 请仔细阅读链接。