マップ上で選択した各ピンの座標を取得しようとしています。選択したすべてのピンで同じ値が得られるという問題があります。
サブタイトルでは、選択した各ピンの値を確認できますが、それを緯度と経度の文字列値と等しくすると、常に同じ値が得られるため、緯度と経度の文字列の値は変化しません。それで、私の問題はどこにあるのでしょうか?
- (void)viewDidLoad{
CLLocationCoordinate2D annotationCoord;
annotationPoint.subtitle = [NSString stringWithFormat:@"%f & %f", annotationPoint.coordinate.latitude, annotationPoint.coordinate.longitude];
//Lat and Lon are FLOAT.
Lat = annotationPoint.coordinate.latitude;
Lon = annotationPoint.coordinate.longitude;
NSLog(@"Lat is: %f and Lon is: %f", Lat, Lon);
}
AppleMapアプリへの道順を取得するために住所に到達するためにこれを行っています。選択したピンの値が必要です。
- (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;
//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;
}
LAT と LON の値は常に同じです。私の問題はどこにあるのでしょうか?
-(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, %f", Lat, Lon);
NSURL *URL = [NSURL URLWithString:str];
[[UIApplication sharedApplication] openURL:URL];
}
ソリューションのテスト:
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
CLLocation *pinLocation = [[CLLocation alloc] initWithLatitude:annotationPoint.coordinate.latitude longitude:annotationPoint.coordinate.longitude];
NSLog(@"RESULT: Lat:%@ Long:%@", [[view annotation]coordinate].latitude,[[view annotation]coordinate].longitude);
}
NSLog の結果:LAST: <+0.00000000,+0.00000000> +/- 0.00m (speed -1.00 mps / course -1.00) @ 8/23/13, 10:01:04 AM Standard Time