2011-10-19 2 views
0

スクロールしても、あなたのMapViewの中央に目印/注釈を持ちますあなたの地図ビュー、あなたが地図をスクロールすると、目印は常に中央にとどまるべきです。あなたは、私がこれを行う方法を把握しようと多くの時間を費やしてきた

私もこれをやっている別のアプリを見ました!

+1

参照[この質問](http://stackoverflow.com/questions/6504536/add-to-add-on-map-in-iphoneの注釈)。 – Anna

+0

@AnnaKareninaありがとう、ちょうど私が探していたものです! – Marc

+0

@Annaこれは答えのようです。あなたはポストをお願いしますので、OPはそれを受け入れることができますか? – NGLN

答えて

1

は答えがありますHow to add annotation on center of map view in iPhone?

で、私の質問が見つかりました:

マップビューの中心部の上方に位置するだけで通常のビューのではなく、実際のアノテーションを使用したい場合は、次のことができます。

  • は、設定可能な座標プロパティ(あらかじめ定義されたMKPointAnnotationクラスなど)を持つアノテーションクラスを使用します。これにより、中心が変更されたときにアノテーションを削除して追加する必要がなくなります。
  • 確認マップビューのデリゲートプロパティが設定されていることを確認し(マップビューのregionDidChangeAnimatedデリゲートメソッドでcenterAnnotation
  • は、その(など、タイトル)座標を更新すると言う、プロパティにそれへの参照を保持
  • のviewDidLoad
  • で を注釈を作成します)

例:

@interface SomeViewController : UIViewController <MKMapViewDelegate> { 
    MKPointAnnotation *centerAnnotation; 
} 
@property (nonatomic, retain) MKPointAnnotation *centerAnnotation; 
@end 

@implementation SomeViewController 

@synthesize centerAnnotation; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    MKPointAnnotation *pa = [[MKPointAnnotation alloc] init]; 
    pa.coordinate = mapView.centerCoordinate; 
    pa.title = @"Map Center"; 
    pa.subtitle = [NSString stringWithFormat:@"%f, %f", pa.coordinate.latitude, pa.coordinate.longitude]; 
    [mapView addAnnotation:pa]; 
    self.centerAnnotation = pa; 
    [pa release]; 
} 

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated { 
    centerAnnotation.coordinate = mapView.centerCoordinate; 
    centerAnnotation.subtitle = [NSString stringWithFormat:@"%f, %f", centerAnnotation.coordinate.latitude, centerAnnotation.coordinate.longitude]; 
} 

- (void)dealloc { 
    [centerAnnotation release]; 
    [super dealloc]; 
} 

@end 

さて、これはスムーズに注釈を移動しなくなります。あなたがよりスムーズに移動する注釈が必要な場合は、マップビューにUIPanGestureRecognizerUIPinchGestureRecognizerを追加しても、ジェスチャーハンドラ内で注釈を更新することができます。

// (Also add UIGestureRecognizerDelegate to the interface.) 

    // In viewDidLoad: 
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; 
    panGesture.delegate = self; 
    [mapView addGestureRecognizer:panGesture]; 
    [panGesture release]; 

    UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; 
    pinchGesture.delegate = self; 
    [mapView addGestureRecognizer:pinchGesture]; 
    [pinchGesture release]; 

- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer 
{ 
    centerAnnotation.coordinate = mapView.centerCoordinate; 
    centerAnnotation.subtitle = [NSString stringWithFormat:@"%f, %f", centerAnnotation.coordinate.latitude, centerAnnotation.coordinate.longitude]; 
} 

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { 
    //let the map view's and our gesture recognizers work at the same time... 
    return YES; 
} 
+0

これは、上の例のようにズームしたときにピンを中央に置いているようには見えません。 –

関連する問題