gpt4 book ai didi

iphone - 如何在 iPhone 中的 map View 中心添加注释?

转载 作者:行者123 更新时间:2023-12-03 18:38:04 26 4
gpt4 key购买 nike

我的应用程序中有 map View ,我想在 map View 的中心添加注释图钉(红色图钉)。
现在,当用户滚动 map View 引脚时,应根据该情况调整中心位置。
如何做到这一点?
谢谢

最佳答案

如果您想使用实际的注释,而不仅仅是位于 map View 中心上方的常规 View ,您可以:

  • 使用具有可设置坐标属性的注释类(例如预定义的MKPointAnnotation类)。这避免了在中心发生变化时必须删除和添加注释。
  • 在viewDidLoad中创建注释
  • 在属性中保留对其的引用,例如 centerAnnotation
  • 在 map View 的 regionDidChangeAnimated 委托(delegate)方法中更新其坐标(和标题等)(确保设置了 map View 的委托(delegate)属性)

示例:

@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 添加到 map View ,并在手势处理程序中更新注释:

    // (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 - 如何在 iPhone 中的 map View 中心添加注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6504536/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com