如何在MapKit中为pin注释命名?

时间:2018-03-21 02:56:11

标签: ios swift mapkit

enter image description here

我想给上面的绿色和右侧引脚注释命名。

我看到一个视频教程,他可以使用annotation.title =为注释命名,但我不知道为什么我可以在我的MapKit中正确显示该名称。

这是我使用的代码

import UIKit
import MapKit

class ViewController: UIViewController, MKMapViewDelegate {

    @IBOutlet weak var mapKit: MKMapView!


    override func viewDidLoad() {
        super.viewDidLoad()

        mapKit.delegate = self

       let bakrieTowerCoordinate = CLLocation(latitude: -6.23860724759536, longitude: 106.789429759178)
       let GBKCoordinate = CLLocation(latitude: -6.23864960081552, longitude: 106.789627819772)


        let locationGBK : CLLocationCoordinate2D = CLLocationCoordinate2DMake(-6.23864960081552, 106.789627819772)
        let locationBakrieToweer : CLLocationCoordinate2D = CLLocationCoordinate2DMake(-6.23860724759536, 106.789429759178)

        let annotation =  MKPointAnnotation()
        annotation.coordinate = locationGBK
        annotation.title = "GBK"
        annotation.subtitle = "Stadion"
        mapKit.addAnnotation(annotation)

        let annotation2 =  MKPointAnnotation()
        annotation2.coordinate = locationBakrieToweer
        annotation2.title = "Bakrie Tower"
        annotation2.subtitle = "Office"
        mapKit.addAnnotation(annotation2)



        zoomMapOn(location1: GBKCoordinate, location2: bakrieTowerCoordinate)


    }



    func zoomMapOn(location1: CLLocation, location2: CLLocation) {

        let distanceOf2CoordinateInMeters =  location1.distance(from: location2)
        let radius = distanceOf2CoordinateInMeters * 3

        let coordinateRegion = MKCoordinateRegionMakeWithDistance(location1.coordinate, radius, radius)
        mapKit.setRegion(coordinateRegion, animated: true)

    }


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

        let annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "pin")

        guard let locationName = annotation.title else {return nil}


        if locationName == "GBK" {
            annotationView.pinTintColor = UIColor.green
        } else if locationName == "Bakrie Tower" {
            annotationView.pinTintColor = UIColor.red
        }


        return annotationView
    }






}

2 个答案:

答案 0 :(得分:1)

将此代码添加到视图控制器 -

import UIKit
import MapKit

class ViewController: UIViewController, MKMapViewDelegate {

    @IBOutlet weak var mapKit: MKMapView!


    override func viewDidLoad() {
        super.viewDidLoad()

        mapKit.delegate = self

        let bakrieTowerCoordinate = CLLocation(latitude: -6.23860724759536, longitude: 106.789429759178)
        let GBKCoordinate = CLLocation(latitude: -6.23864960081552, longitude: 106.789627819772)


        let locationGBK : CLLocationCoordinate2D = CLLocationCoordinate2DMake(-6.23864960081552, 106.789627819772)
        let locationBakrieToweer : CLLocationCoordinate2D = CLLocationCoordinate2DMake(-6.23860724759536, 106.789429759178)

        let annotation =  MKPointAnnotation()
        annotation.coordinate = locationGBK
        annotation.title = "GBK"
        annotation.subtitle = "Stadion"
        mapKit.addAnnotation(annotation)

        let annotation2 =  MKPointAnnotation()
        annotation2.coordinate = locationBakrieToweer
        annotation2.title = "Bakrie Tower"
        annotation2.subtitle = "Office"
        mapKit.addAnnotation(annotation2)


        zoomMapOn(location1: GBKCoordinate, location2: bakrieTowerCoordinate)

    }



    func zoomMapOn(location1: CLLocation, location2: CLLocation) {

        let distanceOf2CoordinateInMeters =  location1.distance(from: location2)
        let radius = distanceOf2CoordinateInMeters * 3

        let coordinateRegion = MKCoordinateRegionMakeWithDistance(location1.coordinate, radius, radius)
        mapKit.setRegion(coordinateRegion, animated: true)

    }


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

        let annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "pin")

        guard let locationName = annotation.title else {return nil}


        if locationName == "GBK" {
            annotationView.canShowCallout = true
        } else if locationName == "Bakrie Tower" {
            annotationView.pinTintColor = UIColor.red
        }

        annotationView.canShowCallout = true // Add this line in your code
        return annotationView
    }

}

当您点按图钉时,它会显示文字,如 - enter image description here enter image description here

添加  annotationView.canShowCallout = true内的mapView(_ mapView:)。谢谢。

答案 1 :(得分:0)

在返回mapView(_:viewFor:)

之前,您需要在annotationView中设置此属性
annotationView.canShowCallout = true

现在,当您点按图钉时,它会显示您的文字。