我花了很多时间试图弄清楚如何做到这一点:
在 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
andUIPinchGestureRecognizer
并更新手势处理程序中的注解:
// (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;
}