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