目标C-第一次添加图钉后注释标题(空),然后第二次正确显示注释

时间:2018-07-11 17:46:41

标签: objective-c location mkannotation cgpoint

我在地图顶部有不同的图钉。他们每个人都有一个注释,显示该位置的地址。第一个是用户位置,它正确显示了地址。第二个允许用户搜索位置,并添加一个将地址显示为注释的图钉。第三个允许用户通过在屏幕上轻击来添加大头针,并且应该将地址显示为注释,但是它仅在用户第二次添加大头针之后才显示,而不是第一次。第一次,NSLog返回(空),第二次之后,NSLog返回地址。任何帮助表示赞赏。

这是我的代码的一部分:

@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);
}

1 个答案:

答案 0 :(得分:1)

在完成处理程序中移动最后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);
    }
}];