0

私はこれを行う方法を理解しようと何時間も費やしました:

ここに画像の説明を入力してください

mapViewのcenterCoordinateに目印/注釈がある場合、地図をスクロールすると、目印は常に中央に留まる必要があります。

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

4

1 に答える 1

1

iPhoneのマップビューの中央に注釈を追加する方法で私の質問を見つけましたか?

答えがあります:

マップビューの中心の上に配置された通常のビューだけでなく、実際の注釈を使用する場合は、次のことができます。

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

例:

@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

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

    // (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;
}
于 2011-11-02T15:40:05.977 に答える