2

タイトルとサブタイトルを表示する注釈付きの mapView があります。字幕が注釈の幅よりも長い場合があるので、複数行にできますか? これまでのところ、次のようにコーディングされています。

func annotate(newCoordinate, title: String, subtitle: String) {
    let annotation = MKPointAnnotation()
    annotation.coordinate = newCoordinate
    annotation.title = title
    annotation.subtitle = subtitle
    self.map.addAnnotation(annotation)    
}

次に、いくつかのオプションを設定します

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {...}

ここでは関係ありません。

カスタム注釈ビューを作成することは可能ですか? 私はいくつかのことを試しましたが、何もうまくいきませんでした。私が得ることができる最も近いのは、長い字幕を個別に表示するボタンを追加することですが、注釈の中に入れたいと思います。

出来ますか?

4

1 に答える 1

11

私はそれを理解し、viewForAnnotationにラベルを追加しましたが、うまくいきました

¯\_(ツ)_/¯

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

    if annotation is MKUserLocation {
        //return nil so map view draws "blue dot" for standard user location
        return nil
    }

    let reuseId = "pin"

    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
    if pinView == nil {
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView!.canShowCallout = true
    }
    else {
        pinView!.annotation = annotation
    }

    //THIS IS THE GOOD BIT
    let subtitleView = UILabel()
    subtitleView.font = subtitleView.font.fontWithSize(12)
    subtitleView.numberOfLines = 0
    subtitleView.text = annotation.subtitle!
    pinView!.detailCalloutAccessoryView = subtitleView


    return pinView
}
于 2016-06-12T10:09:45.823 に答える