我的地图视图上有很多注释,我需要计算从用户位置到录音注释的距离。怎么可能呢?
我的注释
- (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);
}