Agung
Agung

Reputation: 13843

how to give name to pin annotation in the MapKit?

enter image description here

I want to give name to the green and right pin annotation above.

I see a video tutorial, and he can give name to the annotation by using annotation.title = but I don't know why I can get the name correctly show in my MapKit.

here is the code I use

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
    }






}

Upvotes: 0

Views: 1840

Answers (2)

Rashed
Rashed

Reputation: 2425

Add this code to your view controller -

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
    }

}

When you tap on the pin, it will show the text, Like - enter image description here enter image description here

Just Add annotationView.canShowCallout = true inside your mapView(_ mapView:). Thank you.

Upvotes: 2

Mike Taverne
Mike Taverne

Reputation: 9352

You need to set this property in mapView(_:viewFor:) before returning your annotationView:

annotationView.canShowCallout = true

Now when you tap the pin, it will show your text.

Upvotes: 0

Related Questions