mapKit不显示注释

时间:2017-01-24 22:00:12

标签: ios swift swift3 mapkit mkannotationview

我的代码没有显示每个引脚的标题或副标题。引脚出现在那里的坐标上。但是当点击引脚时没有任何显示。我只想将引脚设置为不同的颜色,点击时它们会显示一条消息。

import UIKit
import MapKit

class MyPointAnnotation : MKPointAnnotation {
var pinTintColor: UIColor?
}

class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet var jmap: MKMapView!

override func viewDidLoad() {
    super.viewDidLoad()

    jmap.delegate = self

    let hello = MyPointAnnotation()
    hello.coordinate = CLLocationCoordinate2D(latitude: 40, longitude: -73)
    hello.title = "My Shop"
    hello.subtitle = "its cool"
    hello.pinTintColor = .red


    let hellox = MyPointAnnotation()

    hellox.coordinate = CLLocationCoordinate2D(latitude: 34, longitude: -72)

    hellox.title = "NOW"

    hellox.title = "JUdo"
    hellox.pinTintColor = .blue

    jmap.addAnnotation(hello)
    jmap.addAnnotation(hellox)
}

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "myAnnotation") as? MKPinAnnotationView

    if annotationView == nil {
        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "myAnnotation")
    } else {
        annotationView?.annotation = annotation
    }

    if let annotation = annotation as? MyPointAnnotation {
        annotationView?.pinTintColor = annotation.pinTintColor



    }
    return annotationView
    }}

1 个答案:

答案 0 :(得分:1)

在返回annotationView?.canShowCallout = true

之前添加annotationView