マップビューに多くの注釈があり、ユーザーの位置からテープに記録された注釈までの距離を計算する必要があります。どうすればそれができますか?
マイアノテーション
- (MKAnnotationView *)mapView:(MKMapView *)aMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if (![annotation isKindOfClass:[MyAnnotation class]])
{
return nil;
}
static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
MKAnnotationView *pinView = [aMapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if (pinView == nil)
{
pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
pinView.canShowCallout = YES;
pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeInfoLight];
}
else
pinView.annotation = annotation;
MyAnnotation *myAnn = (MyAnnotation *)annotation;
pinView.image = [UIImage imageNamed:myAnn.icon];
return pinView;
}
計算するには私が使用します
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
CLLocation *pinLocation = [[CLLocation alloc] initWithLatitude:37.322998 longitude:-122.032182];
NSLog(@"New Location:%@", newLocation);
CLLocationDistance distance = [newLocation distanceFromLocation:pinLocation];
NSLog(@"Distance to pin %4.0f", distance);
}
しかし、注釈がテープに記録されたときにピン座標を自動的に取得するにはどうすればよいですか?
編集:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view
calloutAccessoryControlTapped:(UIControl *)control
{
CLLocation *pinLocation = [[CLLocation alloc] initWithLatitude:[(MyAnnotation*)[view annotation] coordinate].latitude longitude:[(MyAnnotation*)[view annotation] coordinate].longitude];
CLLocation *userLocation = [[CLLocation alloc] initWithLatitude:self.mapView.userLocation.coordinate.latitude longitude:self.mapView.userLocation.coordinate.longitude];
CLLocationDistance distance = [pinLocation distanceFromLocation:userLocation];
NSLog(@"Distance to pin %4.0f", distance);
}