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

查看:27
本文介绍了如何在 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:

  • 使用具有可设置坐标属性的注释类(例如预定义的 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

现在这将移动注释但不平滑.如果您需要注释移动得更流畅,您可以在地图视图中添加 UIPanGestureRecognizerUIPinchGestureRecognizer 并更新手势处理程序中的注释:

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天全站免登陆