Dan*_*Dan 3 xcode ios mapbox swift
Mapbox 提供了关于自定义注释图像和自定义注释视图的便捷文档:
https://www.mapbox.com/ios-sdk/examples/annotation-views/ https://www.mapbox.com/ios-sdk/examples/marker-image/
然而,将这些想法结合起来并自定义注释视图的图像似乎是不可能的。基本上我希望做的是有一张照片的注释(用户选择),其中还有一个可以在点击时动画的寄宿生。
有没有人也遇到过这个限制?
MGLAnnotationView继承自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)
}
}
Run Code Online (Sandbox Code Playgroud)
然后在中使用该子类mapView:viewForAnnotation::
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
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2750 次 |
| 最近记录: |