30

Swift で注釈ピンのカスタム アイコンを取得することができましたが、今でも異なる注釈に 2 つの異なる画像を使用して立ち往生しています。現在、ボタンは地図に注釈を追加します。注釈も追加するが別のア​​イコンを持つ別のボタンが必要です。

これに reuseId を使用する方法はありますか?

class ViewController: UIViewController, MKMapViewDelegate {

@IBOutlet weak var Map: MKMapView!

@IBAction func btpressed(sender: AnyObject) {

    var lat:CLLocationDegrees = 40.748708
    var long:CLLocationDegrees = -73.985643
    var latDelta:CLLocationDegrees = 0.01
    var longDelta:CLLocationDegrees = 0.01

    var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
    var location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(lat, long)
    var region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)

    Map.setRegion(region, animated: true)


    var information = MKPointAnnotation()
    information.coordinate = location
    information.title = "Test Title!"
    information.subtitle = "Subtitle"

    Map.addAnnotation(information)
}

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
    if !(annotation is MKPointAnnotation) {
        return nil
    }

    let reuseId = "test"

    var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
    if anView == nil {
        anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        anView.image = UIImage(named:"1.png")
        anView.canShowCallout = true
    }
    else {
        anView.annotation = annotation
    }

    return anView
}
4

2 に答える 2

83

viewForAnnotationデリゲート メソッドで、メソッドが呼び出されるベースを設定します imageannotation

これは、ビューがデキューまたは作成されたに必ず実行してください(if anView == nilパーツ内だけでなく)。それ以外の場合、デキューされたビューを使用する注釈は、以前にビューを使用した注釈の画像を表示します。

基本的なMKPointAnnotationでは、注釈を区別するための 1 つの大雑把な方法はそれらによるものですが、titleこれはあまり柔軟ではありません。

より良い方法は、プロトコルを実装するカスタム アノテーション クラスを使用しMKAnnotation(これを行う簡単な方法は、サブクラス化することですMKPointAnnotation)、カスタム ロジックの実装を支援するために必要なプロパティを追加することです。

カスタム クラスでimageName、注釈に基づいて画像をカスタマイズするために使用できるプロパティを追加します。

この例のサブクラスMKPointAnnotation:

class CustomPointAnnotation: MKPointAnnotation {
    var imageName: String!
}

タイプの注釈を作成CustomPointAnnotationし、それらを設定しますimageName:

var info1 = CustomPointAnnotation()
info1.coordinate = CLLocationCoordinate2DMake(42, -84)
info1.title = "Info1"
info1.subtitle = "Subtitle"
info1.imageName = "1.png"

var info2 = CustomPointAnnotation()
info2.coordinate = CLLocationCoordinate2DMake(32, -95)
info2.title = "Info2"
info2.subtitle = "Subtitle"
info2.imageName = "2.png"

では、プロパティをviewForAnnotation使用してビューの を設定します。imageNameimage

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
    if !(annotation is CustomPointAnnotation) {
        return nil
    }

    let reuseId = "test"

    var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
    if anView == nil {
        anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        anView.canShowCallout = true
    }
    else {
        anView.annotation = annotation
    }

    //Set annotation-specific properties **AFTER**
    //the view is dequeued or created...

    let cpa = annotation as CustomPointAnnotation
    anView.image = UIImage(named:cpa.imageName)

    return anView
}
于 2014-09-03T02:17:12.137 に答える