如何在swift 3中为我的注释添加标题和副标题?

时间:2017-09-02 23:57:21

标签: ios swift swift3 mkmapview mapkit

我试图在我的注释中添加标题和副标题,但我的所有尝试都失败了。请帮帮我!

import UIKit
import MapKit

class MyPointAnnotation : MKPointAnnotation{
    var pinTintColor: UIColor?

}

class MapViewController: UIViewController, MKMapViewDelegate {


    @IBOutlet weak var mapView: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()

        mapView.delegate = self;


        let hello = MyPointAnnotation()
        hello.coordinate = CLLocationCoordinate2D(latitude: 46.771210, longitude: 23.623635)
        hello.title = "tile"
        hello.subtitle = "subtitle"

        hello.pinTintColor = .blue

        let hellox = MyPointAnnotation()
        hellox.coordinate = CLLocationCoordinate2D(latitude: 44.426767, longitude: 26.102538)
        hellox.pinTintColor = .blue

        mapView.addAnnotation(hello)
        mapView.addAnnotation(hellox)
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    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 {
            if #available(iOS 9.0, *) {
                annotationView?.pinTintColor = annotation.pinTintColor
            } else {
                // Fallback on earlier versions
            }
        }

        return annotationView
    }
}

2 个答案:

答案 0 :(得分:0)

您需要设置canShowCallout true才能显示标题和副标题。

请尝试以下代码。

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")
        annotationView?.canShowCallout = true
    } else {
        annotationView?.annotation = annotation
    }

    if let annotation = annotation as? MyPointAnnotation {
        if #available(iOS 9.0, *) {
            annotationView?.pinTintColor = annotation.pinTintColor
        } else {
            // Fallback on earlier versions
        }
    }

    return annotationView
}

答案 1 :(得分:0)

我的问题通过以下代码解决。在swift 4中

 @IBOutlet weak var map: MKMapView! 
override func viewDidLoad() {
 let location = CLLocationCoordinate2D(latitude: 12.956360, longitude: 77.716615)

let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: 12.956360, longitude: 77.716615)
map.setRegion(MKCoordinateRegionMakeWithDistance(location, 1500, 1500), animated: true)
annotation.title = "your title"
annotation.subtitle = "your subtitle"
map.addAnnotation(annotation);

}