我在calloutAccessoryControlTapped
double中分配一个值,Lat
并Lon
在按钮操作中使用它,但我得到Lat
andLon
值为零,而我可以在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];
}