gpt4 book ai didi

objective-c - CLGeocoder 反向地理编码位置。第一次在 [placemarks count = 0]?

转载 作者:行者123 更新时间:2023-12-01 18:00:04 25 4
gpt4 key购买 nike

我是 ObjC 的新手,我正在努力使用 CLGeocoder。我希望能够使用 reverseGeocodeLocation获取一个字符串,其中包含当用户按下完成按钮时我传递给我的委托(delegate)的用户位置。

所以用户触发了一个MapViewController的显示,我在viewDidLoad中调用reverseGeocodeLocation但是 [placemarks count = 0]这是第一次,我没有地标来获取我需要的信息。用户第二次触发 MapViewController 的显示时,placemarks 数组已被填充并且一切正常。

我怀疑这与 reverseGeocodeLocation 是一个异步调用有关 - 但我不知道如何解决这个问题。我曾尝试在线搜索,但似乎没有什么能帮助我理解我做错了什么以及如何解决这个问题。提前致谢。

@interface MapViewController ()
@property (strong, nonatomic) CLGeocoder *geocoder;
@property (readwrite, nonatomic) NSString *theLocationName;
@end

@implementation MapViewController
@synthesize mapView, geocoder, delegate = _delegate, theLocationName = _theLocationName;

- (void)viewDidLoad
{
[super viewDidLoad];

self.mapView.delegate=self;
self.mapView.showsUserLocation = YES;

[self theUserLocation];
}

-(void)theUserLocation
{
if (!geocoder)
{
geocoder = [[CLGeocoder alloc] init];
}

MKUserLocation *theLocation;
theLocation = [self.mapView userLocation];

[geocoder reverseGeocodeLocation:theLocation.location
completionHandler:^(NSArray* placemarks, NSError* error)
{
if ([placemarks count] > 0)
{
CLPlacemark *placemark = [placemarks objectAtIndex:0];

[self setTheLocationName: placemark.locality];

}
}];

- (IBAction)done:(id)sender
{

[[self delegate] mapViewControllerDidFinish:self locationName:[self theLocationName]];

}

@end

最佳答案

这不是您问题的确切答案,但是,如果您可以切换到除 CLGeocoder 之外的其他解决方案,那么以下功能可以帮助您从给定的纬度、经度获取地址

#define kGeoCodingString @"http://maps.google.com/maps/geo?q=%f,%f&output=csv" //define this at top

-(NSString *)getAddressFromLatLon:(double)pdblLatitude withLongitude:(double)pdblLongitude
{
NSString *urlString = [NSString stringWithFormat:kGeoCodingString,pdblLatitude, pdblLongitude];
NSError* error;
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSASCIIStringEncoding error:&error];
locationString = [locationString stringByReplacingOccurrencesOfString:@"\"" withString:@""];
return [locationString substringFromIndex:6];
}

信用: Selected Answer to this question

关于objective-c - CLGeocoder 反向地理编码位置。第一次在 [placemarks count = 0]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11287738/

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