gpt4 book ai didi

iphone:- MKReverseGeocoder 获取 postalCode、subAdministrativeArea、locality 和 subThoroughfare 的空值

转载 作者:行者123 更新时间:2023-11-29 11:05:41 41 4
gpt4 key购买 nike

我正在使用 MKReverseGeocoderlocationManager 的 didUpdateToLocation 委托(delegate) 但是我想知道当我在 设备

我的代码如下:-

- (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)

地区(空)

subLocality(null)

adminisAreaGujarat

subAdminArea(null)

postalCode(null)

国家/地区印度

国家代码IN

使用 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);

}


}];
}

输出是:- ~~~~~~~~(null)

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

最佳答案

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

CLGeoCode Class Reference

Related sample code

关于iphone:- MKReverseGeocoder 获取 postalCode、subAdministrativeArea、locality 和 subThoroughfare 的空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13723825/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com