2012-06-04 9 views
7

Ich habe einen Benutzerstandort (blauer Punkt) und Anmerkungen unter mapView. Wenn Annotation ausgewählt ist, setze ich Text auf distLabel - "Entfernung zu Punkt% 4.0f m.". Wie kann ich diese Textbeschriftung aktualisieren, wenn der Benutzer bewegt?So berechnen Sie die Entfernung vom Benutzerstandort zur Annotation, wenn sich der Benutzer bewegt

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

Ich weiß, es ist eine Funktion didUpdateToLocation, aber wie kann ich es mit didSelectAnnotationView?

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

Antwort

15

Die Kartenansicht hat eine selectedAnnotations Eigenschaft, die Sie in der didUpdateToLocation Methode können Sie erkennen, welche Anmerkung aus der Entfernung zu erhalten.

(By the way, wenn Sie die Karte Ansicht des verwenden userLocation, könnten Sie die Karte Ansicht des didUpdateUserLocation Delegatmethode statt didUpdateToLocation verwenden möchten, das ist ein CLLocationManager Delegatmethode.)

Im Delegatmethode Sie kann prüfen, ob aktuell eine Annotation vorhanden ist, und, falls dies der Fall ist, den Abstand zu dieser Annotation anzeigen (andernfalls "no annotation selected" sagen).

Sie können eine allgemeine Methode schreiben, die sowohl von didSelectAnnotationView als auch didUpdateUserLocation aufgerufen werden kann, um die Codeverdopplung zu reduzieren.

Zum Beispiel:

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

Große Antwort wie von Ihnen immer! Vielen Dank! –

Verwandte Themen