【问题标题】:Need to add a Fixed Overlay like on mapview in IOS需要在 IOS 的 mapview 上添加一个固定的叠加层
【发布时间】:2014-12-10 06:34:57
【问题描述】:
我需要创建一个地图视图界面,类似于 iOS 中的 OLA Cabs 应用程序。我真正想做的是修复 mapView 上的覆盖并允许用户滚动地图视图。为了使叠加层可以固定在用户想要的任何位置,我在 iOS 和 MapKit 中搜索了很多关于叠加层的信息,但无法实现。如果有人能给我实现这一目标的提示,我将不胜感激。这是屏幕截图
此处注释保持固定,您可以在其上移动地图视图,这样当您停止地图视图时,叠加层将指向您停止的新位置
【问题讨论】:
标签:
ios
objective-c
mkmapview
mkannotation
mkoverlay
【解决方案1】:
创建一个修复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"]];
现在必须在您拖动地图视图并添加看起来像注释的图像时删除图像。完成后添加注释并从地图视图中删除图像。
- (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];
}
【解决方案2】:
它不是地图注释覆盖,它是一个普通的UIImageView,它已经放置在MKMapView上,它总是用来获取中心点的经纬度地图。
希望这将是实现您的目标的简单方法。
@Kampai 已为您添加了相同的代码。