如何在iPhone地图视图中心添加注释? [英] How to add annotation on center of map view in iPhone?

查看:219
本文介绍了如何在iPhone地图视图中心添加注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序地图视图和我想在地图视图中心添加注释引脚(引脚红色)。结果
现在,当用户滚动地图视图引脚应按照与中心进行调整。结果
那怎么办?结果
谢谢

I have MAP view in my app and i want to add Annotation pin (Red pin) on center of map view.
Now when user scroll map view pin should adjust with center according to that.
How to do that?
Thank

推荐答案

如果你想使用一个实际的注解,而不是定位在地图视图的中心,只是一个普通视图,您可以:

If you want to use an actual annotation instead of just a regular view positioned above the center of the map view, you can:


  • 使用一个注解类与可设置的坐标属性(pre定义的 MKPointAnnotation 类如)。这样可以避免当中心更改为删除和添加注释。

  • 在viewDidLoad中创建注释

  • 保持在一个属性对它的引用,说centerAnnotation

  • 在地图视图中的 regionDidChangeAnimated 委托方法
  • 更新其坐标(和标题等)(确保地图视图的委托属性设置)

  • use an annotation class with a settable coordinate property (pre-defined MKPointAnnotation class eg). This avoids having to remove and add the annotation when the center changes.
  • create the annotation in viewDidLoad
  • keep a reference to it in a property, say centerAnnotation
  • update its coordinate (and title, etc) in the map view's regionDidChangeAnimated delegate method (make sure map view's delegate property is set)

例如:

@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 地图视图,并更新在手势的处理程序注释:

Now this will move the annotation but not smoothly. If you need the annotation to move more smoothly, you can add a UIPanGestureRecognizer and UIPinchGestureRecognizer to the map view and also update the annotation in the gesture handler:

    // (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;
}

这篇关于如何在iPhone地图视图中心添加注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆