0

我在calloutAccessoryControlTappeddouble中分配一个值,LatLon在按钮操作中使用它,但我得到LatandLon值为零,而我可以在calloutAccessoryControlTapped. 那么请问我的问题在哪里?

H 文件:

@interface LocationViewController
  double Lat;
  double Lon;
}

M 文件:

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{


Lat = [[view annotation]coordinate].latitude;
Lon = [[view annotation]coordinate].longitude;

NSLog(@"Lat: %f AND Lon %f", Lat, Lon); //The value is correct
}

- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{

MKPinAnnotationView *pinAnnotation = nil;

if(annotation != locationMap.userLocation)
{
    static NSString *defaultPinID = @"myPin";

    pinAnnotation = (MKPinAnnotationView *)[locationMap dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
    if ( pinAnnotation == nil )
        pinAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];

    pinAnnotation.canShowCallout = YES;
    pinAnnotation.animatesDrop = YES;
    pinAnnotation.pinColor = MKPinAnnotationColorGreen;
    pinAnnotation.enabled = YES;

    //instatiate a detail-disclosure button and set it to appear on right side of annotation
    UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [infoButton addTarget:self action:@selector(infoButton:) forControlEvents:UIControlEventTouchUpInside];
    pinAnnotation.rightCalloutAccessoryView = infoButton;
}
return pinAnnotation;
}

-(void)infoButton:(id)sender{

  NSString *str = [NSString stringWithFormat:@"http://maps.apple.com/maps?saddr=%f,%f&daddr=%f,%f", Lat,Lon,lat1,lon1];

  NSLog(@"Test %f AND %f", Lat, Lon); //Value of both are zero here. 

  NSURL *URL = [NSURL URLWithString:str];

  [[UIApplication sharedApplication] openURL:URL];
}
4

1 回答 1

2

不要同时为附件按钮实现calloutAccessoryControlTapped委托自定义方法——只做其中一个。

如果你两者都做,地图视图将同时调用两者,在你的情况下,自定义按钮方法在委托方法之前被调用(并且在设置之前LatLon

建议仅使用calloutAccessoryControlTapped委托方法。

  • 将当前的代码infoButton:移到calloutAccessoryControlTapped方法中
  • 删除infoButton:方法
  • addTarget_viewForAnnotation


不推荐,但是,如果您出于某种原因想使用自定义方法infoButton:而不是委托方法:calloutAccessoryControlTapped

  • 删除calloutAccessoryControlTapped委托方法
  • 在中,使用地图视图infoButton:的属性获取对当前选定注解的引用selectedAnnotations

有关自定义方法选项的代码示例,请参阅如何识别哪个引脚被轻敲

于 2013-08-23T12:22:32.130 回答