私はデリゲートで使用MKReverseGeocoder
しlocationManager's
didUpdateToLocation
ていますが、でアプリをテストしているときに、値がnullになるのではないかと思っています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)
私の上記のコードはシミュレーターでは正しく機能しますが、デバイスでは機能しません。:(都市名を取得する適切な方法を教えてください。