将地标/注释放在mapView的中心,即使您滚动也是如此

时间:2011-10-19 20:23:56

标签: android-mapview ios5 mkannotation mkoverlay

我花了很多时间试图弄清楚如何做到这一点:

enter image description here

在mapView的centerCoordinate中有地标/注释,当您滚动地图时,地标应始终位于中心。

我也见过另一个应用程序这样做了!

1 个答案:

答案 0 :(得分:1)

How to add annotation on center of map view in 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

现在这将移动注释但不顺利。如果您需要注释更顺畅地移动,您可以将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;
}