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

现在这将移动注释但不顺利。如果您需要注解更顺畅地移动,您可以在地图视图中添加一个UIPanGestureRecognizerandUIPinchGestureRecognizer并更新手势处理程序中的注解:

    // (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 回答