How to calculate the distance from the user's location to the annotation when the user moves

I have user location (blue dot) and annotations on mapView . When the annotation is selected, I set the text to distLabel - "Distance to point% 4.0f m.". How to update this text label when moving user?

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 didUpdateToLocation function, but how to use it with didSelectAnnotationView ?

 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { //Did update to location } 
+7
source share
1 answer

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

(By the way, if you use the userLocation map userLocation , you might need to use the didUpdateUserLocation delegate display method instead of didUpdateToLocation , which is the CLLocationManager delegation method.)

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

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

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]; } 
+15
source

All Articles