私はこれを行う方法を理解しようと何時間も費やしました:
mapViewのcenterCoordinateに目印/注釈がある場合、地図をスクロールすると、目印は常に中央に留まる必要があります。
私もこれをやっている別のアプリを見ました!
私はこれを行う方法を理解しようと何時間も費やしました:
mapViewのcenterCoordinateに目印/注釈がある場合、地図をスクロールすると、目印は常に中央に留まる必要があります。
私もこれをやっている別のアプリを見ました!
iPhoneのマップビューの中央に注釈を追加する方法で私の質問を見つけましたか?
答えがあります:
マップビューの中心の上に配置された通常のビューだけでなく、実際の注釈を使用する場合は、次のことができます。
MKPointAnnotation
たとえば、事前定義されたクラス)。これにより、中心が変更されたときに注釈を削除および追加する必要がなくなります。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;
}