我开始在一个项目中使用 MapBox。当我使用自定义图像添加注释时,它居中不正确。它看起来比原始引脚低一点。 是否可以更改标注图像的中心点?
它应该如何:
现在的情况:
- (MGLAnnotationImage *)mapView:(MGLMapView *)mapView imageForAnnotation:(id <MGLAnnotation>)annotation {
MGLAnnotationImage *annotationImage = [mapView dequeueReusableAnnotationImageWithIdentifier:@"driverPinId"];
if (!annotationImage) {
UIImage *image = [UIImage imageNamed:@"DriverPin"];
annotationImageWithImage:image reuseIdentifier:@"driverPinId"];
}
return annotationImage;
}
最佳答案
您应该使用 MGLAnnotationView
而不是 MGLAnnotationImage
因为 MGLAnnotationView a descendant of UIView所以你可以像普通的 UIView 一样向它添加任何你想要的东西。
func mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? {
guard annotation is MGLPointAnnotation else {
return nil
}
guard let castAnnotation = annotation as? MapPointMarker else {
return nil
}
if (!castAnnotation.willUseImage) {
return nil;
}
let identifier = castAnnotation.imageName!
var image: UIImage = UIImage(named: castAnnotation.imageName!)!
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
if annotationView == nil {
annotationView = MGLAnnotationView(annotation: annotation, reuseIdentifier: identifier)
let imageView = UIImageView(image: image)
annotationView!.frame = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
annotationView?.addSubview(imageView)
annotationView?.centerOffset = CGVector(dx: 0, dy: -image.size.height / 2.0)
}
return annotationView
}
在上面的代码片段中,我将 UIImageView 添加到 annotationView 并使用 annotationView?.centerOffset = CGVector(dx: 0, dy: -image.size.height/2.0)
向上移动图像。
希望这对您有所帮助。
关于ios - Mapbox Annotation 图像居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37385924/