如何获取录音引脚的坐标以计算用户位置和引脚之间的距离

时间:2012-05-31 19:55:15

标签: ios xcode annotations location distance

我的mapview上有很多注释,我需要计算从用户位置到录音注释的距离。怎么办?

MyAnnotation

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

}

1 个答案:

答案 0 :(得分:2)

实施mapView:annotationView:calloutAccessoryControlTapped:,在点按图钉时会为您提供MKAnnotationView。此注释视图具有annotation属性,其属性为CLLocationCoordinate2D。从坐标中创建CLLocation并与您的其他位置合并,以使用distanceFromLocation获取距离。