2012-06-04 18 views
7

mapView üzerinde bir kullanıcı konumu (mavi nokta) ve ek açıklamalarım var. Açıklama seçildiğinde, metni distLabel olarak ayarlıyorum - "Noktaya mesafe% 4.0f m.". Kullanıcı hareket ettiğinde bu metin etiketini nasıl güncelleyebilirim?Kullanıcı taşındığında kullanıcı konumundan ek açıklamaya nasıl hesaplanır

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

Ben bir işlev didUpdateToLocation olduğunu biliyorum, ama didSelectAnnotationView ile nasıl kullanabilirim?

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

cevap

15

harita görünümü olan mesafeyi almak için hangi ek açıklama anlatmak için didUpdateToLocation yöntemde kullanabileceğiniz bir selectedAnnotations özelliğine sahiptir.

(eğer en userLocation harita görünümünü kullanıyorsanız arada, bir CLLocationManager temsilci yöntemi olan yerine didUpdateToLocation haritası görünümün didUpdateUserLocation temsilci yöntemi kullanmak isteyebilirsiniz.) Temsilci yöntemde

, sen Seçilmiş olan herhangi bir ek açıklamanın olup olmadığını kontrol edebilir ve eğer öyleyse, bu açıklamaya olan mesafeyi gösterebilir (aksi halde "herhangi bir ek açıklama seçilmedi").

Kod çoğaltmasını azaltmak için hem didSelectAnnotationView hem de didUpdateUserLocation'dan çağrılabilecek genel bir yöntem yazmak isteyebilirsiniz. Örneğin

: sizden her zaman olduğu gibi

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

Büyük cevap! Teşekkür ederim! –

İlgili konular