【问题标题】:Having Placemark/Annotation in the center of your mapView, even if you scroll在地图视图的中心有地标/注释,即使你滚动
【发布时间】:2011-10-19 20:23:55
【问题描述】:

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

在您的 mapView 的 centerCoordinate 中有一个地标/注释,当您滚动地图时,地标应该始终保持在中心。

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

【问题讨论】:

标签: android-mapview ios5 mkannotation mkoverlay


【解决方案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;
}

【讨论】:

  • 这似乎并没有像上面的超级示例那样在缩放时使图钉保持居中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-30
  • 2016-02-09
  • 2013-05-08
  • 2016-06-09
  • 1970-01-01
  • 1970-01-01
  • 2011-06-21
相关资源
最近更新 更多