xcode 用户移动时如何计算从用户位置到注释的距离

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10879579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:29:25  来源:igfitidea点击:

How to calculate distance from user location to annotation when user moves

iosxcodeannotationsdistance

提问by Pavel Kaljunen

I have an user location (blue dot) and annotations on mapView. When annotation is selected, I setting text to distLabel- "Distance to point %4.0f m.". How can I update that text label when user moves?

我有一个用户位置(蓝点)和注释mapView。选择注释后,我将文本设置为distLabel-“到点 %4.0f 米的距离。”。当用户移动时如何更新该文本标签?

didSelectAnnotationView:

didSelectAnnotationView:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{

    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];

    [distLabel setText: [NSString stringWithFormat:@"Distance to point %4.0f m.",    
                                                     distance]];
}

I know there is a function didUpdateToLocation, but how can I use it with didSelectAnnotationView?

我知道有一个函数didUpdateToLocation,但是我如何使用它didSelectAnnotationView呢?

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
  //Did update to location
}

回答by

The map view has a selectedAnnotationsproperty that you can use in the didUpdateToLocationmethod to tell which annotation to get the distance from.

地图视图有一个selectedAnnotations属性,您可以在didUpdateToLocation方法中使用该属性来告诉从哪个注释获取距离。

(By the way, if you are using the map view's userLocation, you might want to use the map view's didUpdateUserLocationdelegate method instead of didUpdateToLocationwhich is a CLLocationManagerdelegate method.)

(顺便说一句,如果您使用的是地图视图的userLocation,您可能希望使用地图视图的didUpdateUserLocation委托方法而不是didUpdateToLocationwhich 是CLLocationManager委托方法。)

In the delegate method, you can check if there is any currently selected annotation and, if so, show the distance to that annotation (otherwise say "no annotation selected").

在委托方法中,您可以检查当前是否有任何选定的注释,如果有,则显示到该注释的距离(否则说“未选择注释”)。

You might want to write a common method that can be called from both didSelectAnnotationViewand didUpdateUserLocationto reduce code duplication.

您可能想要编写一个可以从两者调用的通用方法didSelectAnnotationViewdidUpdateUserLocation减少代码重复。

For example:

例如:

-(void)updateDistanceToAnnotation:(id<MKAnnotation>)annotation
{
    if (annotation == nil) 
    {
        distLabel.text = @"No annotation selected";
        return;
    }

    if (mapView.userLocation.location == nil) 
    {
        distLabel.text = @"User location is unknown";
        return;
    }

    CLLocation *pinLocation = [[CLLocation alloc] 
        initWithLatitude:annotation.coordinate.latitude
               longitude:annotation.coordinate.longitude];

    CLLocation *userLocation = [[CLLocation alloc] 
        initWithLatitude:mapView.userLocation.coordinate.latitude 
               longitude:mapView.userLocation.coordinate.longitude];

    CLLocationDistance distance = [pinLocation distanceFromLocation:userLocation];

    [distLabel setText: [NSString stringWithFormat:@"Distance to point %4.0f m.", distance]];
}

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    if (mapView.selectedAnnotations.count == 0)
        //no annotation is currently selected
        [self updateDistanceToAnnotation:nil];
    else
        //first object in array is currently selected annotation
        [self updateDistanceToAnnotation:[mapView.selectedAnnotations objectAtIndex:0]];
}

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{    
    [self updateDistanceToAnnotation:view.annotation];
}