iOS:是否可以在地图的中心放置注释并在注释始终保持在中心的同时移动地图?

问题描述 投票:0回答:3

我不知道从哪里开始,但我想知道是否可以这样做。我知道如何将mapview中的注释放在中心。这就是我到目前为止所做的。如果有人知道如何在我改变地区时将注释调整到地图的中心,我会非常感激。

  - (IBAction)recenter:(id)sender {
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;
  }


 - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
centerAnnotation.coordinate = self.mapView.centerCoordinate;
centerAnnotation.subtitle = [NSString stringWithFormat:@"%f, %f", centerAnnotation.coordinate.latitude, centerAnnotation.coordinate.longitude];
   }
ios iphone objective-c mkmapview mkannotation
3个回答
1
投票

不是使用注释,而是将子视图添加到包含地图的视图中,并将2个视图的centers设置为彼此相等。当用户平移地图时,它看起来就像是注释正在移动。

然后,只要您需要找到中心“注释”指向的坐标,就可以使用地图视图的centerCoordinate属性。

我已经在地图框和谷歌地图中完成了这项工作,但在MapKit中没有,但我认为它应该可以正常工作。


1
投票

那么注释是不是真的与一个特定的坐标相关联?您始终可以创建MKPinAnnotationView的实例,并将其作为子视图(而不是注释)添加到地图视图中。既然它是UIView的子视图,那应该可以正常工作。


0
投票

SWIFT 4

您可以使用此代码,但不能保留在中心,仅用于中心注释:

// set 20% less latitude under the center point
userLocation.latitude = userLocation.latitude - self.mapView.region.span.latitudeDelta * -0.20

// You can use this method to center map or others...             
let viewRegion = MKCoordinateRegion(center: userLocationModified, span: coordinateSpan)
mapView.setRegion(viewRegion, animated: true)

如果需要添加静态视图,可以添加简单的子视图。

© www.soinside.com 2019 - 2024. All rights reserved.