6

マップに注釈を追加しようとしています。内部に座標を持つ点の配列があります。それらの座標から注釈を追加しようとしています。

私はこれを定義しました:

var points: [CLLocationCoordinate2D] = [CLLocationCoordinate2D]() 
let annotation = MKPointAnnotation()

ポイントの内部には座標があります。私がチェックしました。そして、私はこれを行います:

for index in 0...points.count-1 {
        annotation.coordinate = points[index]
        annotation.title = "Point \(index+1)"
        map.addAnnotation(annotation)
    }

最後の注釈のみを追加し続けます...それらすべてではなく。どうしてこれなの?ところで、タイトルなどで指定したアノテーションを削除する方法はありますか?

4

2 に答える 2

6

各注釈は新しいインスタンスである必要があります。インスタンスを 1 つだけ使用し、その座標をオーバーライドしています。だからあなたのコードを変更してください:

for index in 0...points.count-1 {
    let annotation = MKPointAnnotation()  // <-- new instance here
    annotation.coordinate = points[index]
    annotation.title = "Point \(index+1)"
    map.addAnnotation(annotation)
}
于 2016-11-09T18:34:27.263 に答える