目标C-第一次添加接点时的注释标题(null),然后第二次正确显示注释



我在地图顶部有不同的引脚。它们中的每一个都有一个显示该位置地址的注释。第一个是用户位置,它正确地显示了地址。第二个可以让用户搜索位置,并添加一个显示地址的pin作为注释。第三种方法允许用户通过点击屏幕来添加pin,并将地址显示为注释,但它只在用户第二次添加pin后显示,而不是第一次。NSLog第一次返回(null(,第二次返回地址之后。感谢您的帮助。

这是我代码的一部分:

@implementation SearchAddressVC {
NSString *address;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.mapView.delegate = self;
self.searchBar.delegate = self;
if(locationManager == nil){
locationManager = [[CLLocationManager alloc] init];
if(NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_8_0){
[locationManager requestWhenInUseAuthorization];
[locationManager requestAlwaysAuthorization];
}
}
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = 500;
[locationManager startUpdatingLocation];
////
UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(addPin:)];
recognizer.minimumPressDuration = 0.5;
[self.mapView addGestureRecognizer:recognizer];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
self.geocoder = [[CLGeocoder alloc] init]; 
}
- (void)addPin:(UIGestureRecognizer *)recognizer {
if (recognizer.state != UIGestureRecognizerStateBegan) {
return;
}
CGPoint userTouch = [recognizer locationInView:self.mapView];
[self.mapView removeAnnotations:[self.mapView annotations]];
Location *point = [[Location alloc] init];
point.coordinate = [self.mapView convertPoint:userTouch toCoordinateFromView:self.mapView];
[[GMSGeocoder geocoder] reverseGeocodeCoordinate:CLLocationCoordinate2DMake(point.coordinate.latitude, point.coordinate.longitude) completionHandler:^(GMSReverseGeocodeResponse* response, NSError* error) {
if (error == nil) {
GMSReverseGeocodeResult *result = response.firstResult;
NSString *street = result.lines[0];
NSString *secAdd = result.lines[1];
address = [NSString stringWithFormat:@"%@n%@",
street,
secAdd];
}
}];
point.title = address;
[self.mapView addAnnotation:point];
[self.mapView selectAnnotation:point animated:YES];
NSLog(@"%@",point.title);
}

移动完成处理程序中的最后4行代码。我猜第一次它不工作是因为GMSGeocoder需要一些时间来初始化。这将导致在添加注释后调用完成块。

我认为您的completionHandler代码应该是这样的:

[[GMSGeocoder geocoder] reverseGeocodeCoordinate:CLLocationCoordinate2DMake(point.coordinate.latitude, point.coordinate.longitude) completionHandler:^(GMSReverseGeocodeResponse* response, NSError* error) {
if (error == nil) {
GMSReverseGeocodeResult *result = response.firstResult;
NSString *street = result.lines[0];
NSString *secAdd = result.lines[1];
address = [NSString stringWithFormat:@"%@n%@",
street,
secAdd];
point.title = address;
[self.mapView addAnnotation:point];
[self.mapView selectAnnotation:point animated:YES];
NSLog(@"%@",point.title);
}
}];

相关内容

最新更新