Elfuthark
Elfuthark

Reputation: 261

Turn by turn navigation to annotation with Apple Maps

I have one Annotation on my map showing a business location and one button that says get directions, I'm struggling to get the button to open Apple Maps for me with directions to the Annotations location. Here is the code I have done so far:

import UIKit
import MapKit


class FourthViewController: UIViewController , MKMapViewDelegate {

    @IBOutlet weak var map: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let latitude: CLLocationDegrees = 54.647115
        let longitude: CLLocationDegrees = -6.659070

        let lanDelta: CLLocationDegrees = 0.05

        let lonDelta: CLLocationDegrees = 0.05

        let span = MKCoordinateSpan(latitudeDelta: lanDelta, longitudeDelta: lonDelta)

        let coordinates = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)

        let region = MKCoordinateRegion(center: coordinates, span: span)

        map.setRegion(region, animated: true)

        let annotation = MKPointAnnotation()

        annotation.title = "Pose Beauty Salon"

        annotation.subtitle = "100 Moneyhaw Road"

        annotation.coordinate = coordinates

        map.addAnnotation(annotation)
    }


    @IBAction func mapType(_ sender: AnyObject) {

        switch (sender.selectedSegmentIndex) {
        case 0:
            map.mapType = .standard
        case 1:
            map.mapType = .satellite
        default: // or case 2
            map.mapType = .hybrid
        }

    }

    @IBAction func getDirections(_ sender: AnyObject) {


    }
}

I've also seen annotations when clicked on that shows more info such as business names, addresses, phone numbers, and URLs is this hard to add also?

Upvotes: 2

Views: 244

Answers (1)

Elfuthark
Elfuthark

Reputation: 261

This is the code I used to resolve the issue:

let latitude: CLLocationDegrees = 54.647115
        let longitude: CLLocationDegrees = -6.659070
        let url = URL(string: "https://www.posebeautysalon.com")


        let regionDistance:CLLocationDistance = 10000
        let coordinates = CLLocationCoordinate2DMake(latitude, longitude)
        let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, regionDistance, regionDistance)
        let options = [
            MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center),
            MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span)
        ]
        let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
        let mapItem = MKMapItem(placemark: placemark)
        mapItem.name = "Pose Beauty Salon"
        mapItem.phoneNumber = "+442886737777"
        mapItem.url = url
        mapItem.openInMaps(launchOptions: options)

Upvotes: 1

Related Questions