2017-02-02 8 views
0

私の問題はこの1つに似ています Displaying custom multiple pins shows wrong pins for locations しかし少し異なり、私はSwiftで働いています。変換しても、私は自分の問題を解決できませんでした。 それぞれに異なる画像の注釈があります。画像には番号付きポインタがあります。各注釈の詳細はplistから得られます。タイトルは "1駐車場"、 "2ミル"などです。スペースの前の部分を取って、pngイメージに対応する番号を取得します。注釈イメージが再利用後に変更される

読み込み時に、すべての注釈が正しい画像を持ちます。もし私がその区域から離れて戻ってくると、イメージはお互いに交換することができます。それは再利用の問題だと思われますが、解決策が見えません。 getMapAnnotations()はviewdidloadで呼び出されます。

func getMapAnnotations() -> [Stands] { 
    var annotations:Array = [Stands]() 
    print("ANNOTATIONS") 
    //load plist file 
    var stands: NSArray? 
    if let path = Bundle.main.path(forResource: "stands", ofType: "plist") { 
     stands = NSArray(contentsOfFile: path) 
     } 
    //iterate and create annotations 
    if let items = stands { 
     for item in items { 
     let lat = (item as AnyObject).value(forKey: "lat") as! Double 
     let long = (item as AnyObject).value(forKey: "long")as! Double 
     let annotation = Stands(latitude: lat, longitude: long) 
     let tit = (item as AnyObject).value(forKey: "title") as! String 
     let numb = (item as AnyObject).value(forKey: "no") as! Int 
     annotation.title = "\(numb) \(tit)" 
     annotation.no = numb 
     annotations.append(annotation) 
     } 
    } 
    return annotations 
    } 

その後、注釈ビュー

func mapView(_ mapView: MKMapView!, viewFor annotation: MKAnnotation!) -> MKAnnotationView! { 


    let identifier = "Stand" 


    var imageI: String? 
    var imageAn: String? 
    imageI = annotation.title! 
    if annotation.isKind(of: Stands.self) { 
     print("ANNOTATION \(imageI!)") 
     if let range = imageI?.components(separatedBy: " ") { 

     var imageII = range[0] 
     imageAn = "\(imageII).png" 
     print (imageAn!) 
     } 

     var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) 

     if annotationView == nil { 

     annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier) 

     // 
     annotationView?.image = UIImage(named: imageAn!) 
     annotationView?.canShowCallout = true 
     annotationView?.layer.zPosition = -1 

     let btn = UIButton(type: .detailDisclosure) 
     annotationView?.rightCalloutAccessoryView = btn 
     } else { 

     annotationView!.annotation = annotation 
     } 

     return annotationView 
    } 

    return nil 
    } 

答えて

0

私は私がやっている知っているよりも、試行錯誤により溶液、より多くのを発見したように見えます。 ? 私は を追加annotationView .image = UIImage(命名:!imageAn)

}else{ 
annotationView?.image = UIImage(named: imageAn!) 
annotationView.annotation = annotation 
} 

を作るために私が正しく理解していれば、注釈は表示に関する情報と座標、タイトルとサブタイトルの情報です。 AnnotationViewは、注釈の表示方法を設定します。ピンまたはカスタム画像で。 AnnotationViewがnillの場合、注釈ビューは正しく作成されます。注釈が見えなくなり、戻ってくる場合、AnnotationViewは無限ではないので、システムはそれを表示するためにランダムにビューを取ります。 annotationViewを追加する.image = UIImage(名前:imageAn!) は、正しいイメージを強制的に表示します。 このスキーマが正しくないと誰かが私を修正すると確信しています。

+0

同じことをやりたいと思っている人、つまり番号の付いた注釈があれば、私は今や攻撃ラインを変更しました。この質問は私にその考えを与えました。 http://stackoverflow.com/questions/9822756/replace-icon-pin-by-text-label-in-annotation タイトル(私はちょうど数字を作った)で小さな四角形を表示しています。 – cpmac

関連する問題