2012-06-04 5 views
7

私はユーザーの場所(青い点)と注釈をmapViewに持っています。注釈が選択されている場合、テキストをdistLabelに設定しています - 「%までの距離は4.0fmです」。ユーザーが移動すると、そのテキストラベルをどのように更新できますか?ユーザーが移動したときにユーザーの場所から注釈までの距離を計算する方法

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でそれを使うことができますか?

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

答えて

15

マップビューは、あなたからの距離を取得するためにどの注釈を伝えるためにdidUpdateToLocation方法で使用することができますselectedAnnotations性質を持っています。デリゲートメソッドで

(マップビューのuserLocationを使用している場合はところで、あなたはCLLocationManagerデリゲートメソッドである代わりにdidUpdateToLocationのマップビューのdidUpdateUserLocationデリゲートメソッドを使用する場合があります。)

、あなたは現在選択されている注釈があるかどうかをチェックし、そうであればその注釈までの距離を表示することができます(そうでない場合は「注釈が選択されていません」)。

コードの重複を減らすために、didSelectAnnotationViewdidUpdateUserLocationの両方から呼び出すことができる共通の方法を記述することをお勧めします。例えば

:あなたからいつものように

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

グレート答え!ありがとうございました! –

関連する問題