如何在iPhone中添加地图视图中心的注释?

问题描述:

我在我的应用程序中有MAP视图,我想在地图视图中心添加注释针(红色针)。
现在当用户滚动地图视图引脚应该按照中心来调整。
如何做到这一点?
感谢如何在iPhone中添加地图视图中心的注释?

+0

这意味着你被改变中心改变销的位置。 –

+0

@Muhammad Zeeshan - 是的,当用户滚动地图时,红色的别针(注解)应该改变以适应用户可见的地图视图的新中心。 – dks1725

+0

尝试将注释放到地图中心 –

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

  • 使用注解类与设定的坐标属性(预-defined MKPointAnnotation class eg)。这避免了在中心改变时必须移除和添加注释。
  • 在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; 
} 
+0

感谢它的工作完美.. – dks1725

+0

从我的+1,仍然高于解决方案并没有给出“仍然”的注释,但也是一个完美的基础以及提到在地图解决方案上方查看。 –

+0

有人可以用swift写这段代码:)? –

安娜的回答是聪明的做法,以保持在中心的注解使用标准mapview方式中的注释来映射。然而,正如一位评论者指出的那样,滚动和捏合虽然更好,但仍然显示出明显的滞后,推荐的方法是将注释视图添加为mapview的子视图。这是看起来像。

@interface SHCenterPinMapViewController() <MKMapViewDelegate> 

@property (weak, nonatomic) IBOutlet MKMapView *mapView; 
@property (strong, nonatomic) MKPointAnnotation *centerAnnotaion; 
@property (strong, nonatomic) MKPinAnnotationView *centerAnnotationView; 

@end 

@implementation SHCenterPinMapViewController 

- (MKPointAnnotation *)centerAnnotaion 
{ 
    if (!_centerAnnotaion) { 
     _centerAnnotaion = [[MKPointAnnotation alloc] init]; 
    } 

    return _centerAnnotaion; 
} 

- (MKPinAnnotationView *)centerAnnotationView 
{ 
    if (!_centerAnnotationView) { 
     _centerAnnotationView = [[MKPinAnnotationView alloc] initWithAnnotation:self.centerAnnotaion 
                   reuseIdentifier:@"centerAnnotationView"]; 
    } 

    return _centerAnnotationView; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.mapView.delegate = self; 
    [self.mapView addSubview:self.centerAnnotationView]; 
} 

-(void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 
    [self moveMapAnnotationToCoordinate:self.mapView.centerCoordinate]; 
} 

// These are the constants need to offset distance between the lower left corner of 
// the annotaion view and the head of the pin 
#define PIN_WIDTH_OFFSET 7.75 
#define PIN_HEIGHT_OFFSET 5 

- (void)moveMapAnnotationToCoordinate:(CLLocationCoordinate2D) coordinate 
{ 
    CGPoint mapViewPoint = [self.mapView convertCoordinate:coordinate toPointToView:self.mapView]; 

    // Offset the view from to account for distance from the lower left corner to the pin head 
    CGFloat xoffset = CGRectGetMidX(self.centerAnnotationView.bounds) - PIN_WIDTH_OFFSET; 
    CGFloat yoffset = -CGRectGetMidY(self.centerAnnotationView.bounds) + PIN_HEIGHT_OFFSET; 

    self.centerAnnotationView.center = CGPointMake(mapViewPoint.x + xoffset, 
                mapViewPoint.y + yoffset); 
} 

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated 
{ 
    self.centerAnnotaion.coordinate = mapView.centerCoordinate; 
    [self moveMapAnnotationToCoordinate:mapView.centerCoordinate]; 
} 

@end 

唯一真正有趣的事情不在于你有一定的量,以抵消MKPinAnnotationView占左下角和销头之间的距离。我不喜欢在代码中使用这些与资产相关的常量,因此如果任何人都可以找到更好的方法来实现这一点,那么我完全可以接受。

我创建了一个带有地图控制器的github项目,它可以做到这一点,以及其他一些与使用mapview来让用户选择一个位置相关的东西。看看这里:https://github.com/scottrhoyt/CenterPinMapViewController

+0

这太好了。谢谢。有关如何将缩放锁定在引脚上的任何想法?因此,缩放时只能放大引脚,反之亦然。 http://stackoverflow.com/questions/25577813/make-mkmapview-only-zoom-in-on-the-centercoordinate/25577949#25577949 –

斯威夫特版本

在类:

​​

在viewDidLoad中:

if #available(iOS 9.0, *) { 
     centerAnnotationView.pinTintColor = customColor 
    } else { 
     // Fallback on earlier versions 
     centerAnnotationView.pinColor = MKPinAnnotationColor.Red 
    } 
self.view.addSubview(centerAnnotationView) 

在viewDidAppear:

self.moveMapAnnotationToCoordinate(self.mapView.centerCoordinate) 

然后:

func moveMapAnnotationToCoordinate(locate : CLLocationCoordinate2D) { 
    let mapViewPoint : CGPoint = self.mapView.convertCoordinate(locate, toPointToView: self.mapView) 
    let pinWidth : CGFloat = 7.75 
    let pinHeight : CGFloat = 7 
    let xOffset : CGFloat = CGRectGetMidX(self.centerAnnotationView.bounds) - pinWidth 
    let yOffset : CGFloat = CGRectGetMidY(self.centerAnnotationView.bounds) - pinHeight 

    self.centerAnnotationView.center = CGPointMake(mapViewPoint.x - xOffset, mapViewPoint.y - yOffset) 
} 

对于使用中的位置变化,增加委托CLLocationManagerDelegate然后:

func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) { 
    self.centerAnnotation.coordinate = self.mapView.centerCoordinate 
    self.moveMapAnnotationToCoordinate(self.mapView.centerCoordinate) 
}