2011-08-15 11 views
1

マップビューを実装して、ユーザーが住所を検索したときにアノテーションを配置します。しかし、どういうわけか、注釈はいつかは移動して新しい座標に更新されません。マップをズームした後にのみ、新しい場所に更新されます。字幕は更新されました。なぜ私のマップ注釈は移動しませんでしたか?

- (void)searchBarSearchButtonClicked:(UISearchBar *)theSearchBar { 
    SVGeocoder *geocodeRequest = [[SVGeocoder alloc] initWithAddress:searchBar.text inRegion:@"sg"]; 
    [geocodeRequest setDelegate:self]; 
    [geocodeRequest startAsynchronous]; 
} 

- (void)geocoder:(SVGeocoder *)geocoder didFindPlacemark:(SVPlacemark *)placemark { 
     if (annotation) { 
      [annotation moveAnnotation:placemark.coordinate]; 
      annotation.subtitle = [NSString 
            stringWithFormat:@"%@", placemark.formattedAddress]; 
     } 
     else { 
      annotation = [[MyAnnotation alloc] 
          initWithCoordinate:placemark.coordinate 
          title:@"Tap arrow to use address" 
          subtitle:[NSString 
            stringWithFormat:@"%@", placemark.formattedAddress]]; 
      [mapView addAnnotation:annotation]; 
     } 
    MKCoordinateSpan span; 
    span.latitudeDelta = .001; 
    span.longitudeDelta = .001; 
    MKCoordinateRegion region; 
    region.center = placemark.coordinate; 
    region.span = span; 
    [mapView setRegion:region animated:TRUE]; 

    [searchBar resignFirstResponder]; 
} 

答えて

1

あなたが示したコードの中には、注釈の位置が変更されたことがmapViewに示されていません。注釈自体は、おそらく-moveAnnotationでそれを行うことはできません。なぜなら、アノテーションは一般的に、どのマップやマップに追加されているのか(そうでなければならないのか)分からないからです。

アノテーションを移動する正しい方法は、アノテーションを使用しているMKMapViewからアノテーションを削除し、その位置を更新してマップに追加することです。注釈がマップに追加された後は、その注釈の場所をキャッシュしたり、その注釈をその場所に従って並べ替えたりすることができるだけで、注釈の場所を変更することはできません。また、MKMapViewには、 。

私はこのような何かにあなたの条件を変更したい:

if (annotation == nil) { 
    annotation = [[MyAnnotation alloc] init]; 
    annotation.title = @"Tap arrow to use address"; 
} 
[mapView removeAnnotation:annotation]; 
[annotation moveAnnotation:placemark.coordinate]; 
annotation.subtitle = placemark.formattedAddress; 
[mapView addAnnotation:annotation]; 

これは-initWithCoordinate:title:subtitle:の代わりに-initを呼び出しても安全だと仮定します。もしそうでなければ、あなたはそれを変更したいでしょう。

2

私はMKMapViewが注釈の位置の変更を通知されるとは思わないと思います。 MKAnnotationのsetCoordinate:のドキュメントには、「ドラッグをサポートするアノテーションは、このメソッドを実装して注釈の位置を更新する必要があります。この方法の唯一の目的は、ピンのドラッグをサポートすることだと思われます。

注釈をマップビューから削除してから、座標を変更してマップビューに追加し直してください。

関連する問題