2017-03-24 8 views
0

Mapboxは、注釈のイメージをカスタマイズし、注釈のビューをカスタマイズする上で便利なドキュメントを提供するカスタムに画像を追加することが可能ですこれらのアイデアを組み合わせてアノテーションビューのイメージをカスタマイズすることは可能です。基本的に私がやりたいことは、(ユーザーが選んだ)写真の注釈を持っていることです。タップしたときにアニメーションできるボーダーもあります。はMapBoxでMGLAnnotationView(iOSの、スウィフト)

誰もこの制限に遭遇しましたか?

答えて

1

MGLAnnotationViewUIViewから継承されているため、ビューに画像を追加するために使用するほとんどのテクニックもここで動作します。

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) 
    } 
} 

そしてmapView:viewForAnnotation:でそのサブクラスを使用します:

簡単な方法は、カスタムMGLAnnotationViewサブクラスにサブビューとしてUIImageViewを追加することです

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 
} 
関連する問題