是否可以将图像添加到MapBox(iOS、Swift)中的自定义MGLAnnotationView
Mapbox提供了有关自定义注释图像和自定义注释视图的便捷文档: 然而,似乎不可能将这些想法结合起来并定制注释视图的图像。基本上,我希望做的是有一个照片(用户选择)的注释,它也有一个可以在点击时设置动画的边框 有没有人也遇到过这种限制 继承自是否可以将图像添加到MapBox(iOS、Swift)中的自定义MGLAnnotationView,ios,swift,xcode,mapbox,Ios,Swift,Xcode,Mapbox,Mapbox提供了有关自定义注释图像和自定义注释视图的便捷文档: 然而,似乎不可能将这些想法结合起来并定制注释视图的图像。基本上,我希望做的是有一个照片(用户选择)的注释,它也有一个可以在点击时设置动画的边框 有没有人也遇到过这种限制 继承自UIView,因此,在视图中添加图像所使用的大多数技术在这里也适用 一种简单的方法是将UIImageView添加为自定义MGLAnnotationView子类中的子视图: class CustomImageAnnotationView: MGLAnnot
UIView
,因此,在视图中添加图像所使用的大多数技术在这里也适用
一种简单的方法是将UIImageView
添加为自定义MGLAnnotationView
子类中的子视图:
class CustomImageAnnotationView: MGLAnnotationView {
var imageView: UIImageView!
required init(reuseIdentifier: String?, image: UIImage) {
super.init(reuseIdentifier: reuseIdentifier)
self.imageView = UIImageView(image: image)
self.addSubview(self.imageView)
self.frame = self.imageView.frame
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override init(frame: CGRect) {
super.init(frame: frame)
}
}
然后在中使用该子类:
您好@friedbunny,您的回答对我很有帮助,但我没有什么问题,因为我对mapbox完全陌生,如果我已经有了另一个可重用的标识符,即“annotation.coordinate.longitude”,我想用图像覆盖整个注释,并根据某种类型在注释上显示不同的图像,比如说位置注释的类别。嗨,我得到了基于类型(比如酒吧、健身房、商店等)在注释上显示不同图像的解决方案,但无法摆脱在完整注释上显示图像。
func mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? {
guard annotation is MGLPointAnnotation else {
return nil
}
let imageName = "someImageThatYouHaveAddedToYourAppBundle"
// Use the image name as the reuse identifier for its view.
let reuseIdentifier = imageName
// For better performance, always try to reuse existing annotations.
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier)
// If there’s no reusable annotation view available, initialize a new one.
if annotationView == nil {
annotationView = CustomImageAnnotationView(reuseIdentifier: reuseIdentifier, image: UIImage(named: imageName)!)
}
return annotationView
}
func mapView( _ mapView: MGLMapView, imageFor annotation: MGLAnnotation ) -> MGLAnnotationImage? {
var annotationImage : MGLAnnotationImage? = nil
annotationImage = MGLAnnotationImage( image:UIImage( named: "imageyouwanttoset", reuseIdentifier: annotation.title ) )
return annotationImage
}