1

我的地图视图上有很多注释,我需要计算从用户位置到录音注释的距离。怎么可能呢?

我的注释

- (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);

}
4

1 回答 1

2

实施mapView:annotationView:calloutAccessoryControlTapped:,它会给你一个MKAnnotationView当一个针被敲击的时候。这个注释视图有一个annotation属性,它有一个CLLocationCoordinate2D. 从坐标中,制作一个CLLocation并与您的其他位置结合以获得使用 的距离distanceFromLocation

于 2012-05-31T20:14:47.280 回答