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

How to calculate distance from user location to annotation when user moves(用户移动时如何计算用户位置到注释的距离)
本文介绍了用户移动时如何计算用户位置到注释的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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?

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

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

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

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

推荐答案

地图视图有一个 selectedAnnotations 属性,您可以在 didUpdateToLocation 方法中使用该属性来判断哪个注解获取距离.

The map view has a selectedAnnotations property that you can use in the didUpdateToLocation method to tell which annotation to get the distance from.

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

(By the way, if you are using the map view's userLocation, you might want to use the map view's didUpdateUserLocation delegate method instead of didUpdateToLocation which is a CLLocationManager delegate method.)

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

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").

您可能希望编写一个可以从 didSelectAnnotationViewdidUpdateUserLocation 调用的通用方法,以减少代码重复.

You might want to write a common method that can be called from both didSelectAnnotationView and didUpdateUserLocation to reduce code duplication.

例如:

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

这篇关于用户移动时如何计算用户位置到注释的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

iOS refresh annotations on mapview(iOS在mapview上刷新注释)
In which case that mapView:viewForAnnotation: will be called?(在这种情况下 mapView:viewForAnnotation: 会被调用吗?)
Why does a custom MKMapView annotation image disappear on touch?(为什么自定义的 MKMapView 注释图像在触摸时会消失?)
What#39;s the meaning of new @SystemApi annotation, any difference from @hide?(新的@SystemApi 注解是什么意思,和@hide 有什么区别?)
Retrofit @body with @multipart having Issue(使用 @multipart 改造 @body 有问题)
iPhone Map Kit cluster pinpoints(iPhone Map Kit 集群定位)