需要在IOS中的mapview上添加固定叠加层

时间:2014-12-10 06:34:57

标签: ios objective-c mkmapview mkannotation mkoverlay

我需要创建一个地图视图界面,​​这类似于iOS中的OLA Cabs应用程序。我真正想做的是在mapView上修复叠加层,并允许用户在其上滚动地图视图。因此,可以在用户想要的任何位置修复叠加层,我在iOS和MapKit中搜索了大量有关叠加层的内容,但无法实现。如果有人可以给我实现这个的提示,我将非常感激。这是屏幕的快照

enter image description here

此处注释保持不变,您可以在其上移动地图视图,这样当您停止地图视图时,叠加层将指向您停止的新位置

2 个答案:

答案 0 :(得分:6)

Click here to download demo...

enter image description here

创建修复MKAnnotation和图像视图对象,以在地图视图中设置位置更改效果的动画。

@property (nonatomic, strong) CustomAnnotation      *fixAnnotation;
@property (nonatomic, strong) UIImageView           *annotationImage;

viewDidLoad()方法中添加此代码:

 // Fix annotation
    _fixAnnotation = [[CustomAnnotation alloc] initWithTitle:@"Fix annotation" subTitle:@"Location" detailURL:nil location:self.mapView.userLocation.coordinate];
    [self.mapView addAnnotation:self.fixAnnotation];

    // Annotation image.
    CGFloat width = 64;
    CGFloat height = 64;
    CGFloat margiX = self.mapView.center.x - (width / 2);
    CGFloat margiY = self.mapView.center.y - (height / 2) - 32;
    // 32 is half size for navigationbar and status bar height to set exact location for image.

    _annotationImage = [[UIImageView alloc] initWithFrame:CGRectMake(margiX, margiY, width, height)];
    [self.annotationImage setImage:[UIImage imageNamed:@"mapannotation.png"]];

现在,当您拖动地图视图并添加看起来像注释的图像时,必须删除图像。完成后添加注释并从Map View中删除图像。

- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated {
    NSLog(@"Region will changed...");
    [self.mapView removeAnnotation:self.fixAnnotation];
    [self.mapView addSubview:self.annotationImage];

}


- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
    NSLog(@"Region did changed...");
    [self.annotationImage removeFromSuperview];
    CLLocationCoordinate2D centre = [mapView centerCoordinate];
    self.fixAnnotation.coordinate = centre;
    [self.mapView addAnnotation:self.fixAnnotation];
}

答案 1 :(得分:3)

它不是一个地图注释叠加层,它是一个普通的 UIImageView ,它被放置在 MKMapView 之上,并且它总是用于得到lat-long的中心点地图。

希望这是实现目标的简单方法。

@Kampai为您添加了相同的代码。