0

I am trying to open automatically the Maps App with Navigation, when pressing a Button.

My ViewController look like this:

import UIKit
import MapKit

class WhereViewController: UIViewController {


@IBOutlet weak var MapView: MKMapView!
@IBAction func navigateButtonHandler(_ sender: UIButton) {
    func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView,
                 calloutAccessoryControlTapped control: UIControl) {
        let location = view.annotation as! Artwork
        let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
        location.mapItem().openInMaps(launchOptions: launchOptions)
    }
}


override func viewDidLoad() {
    super.viewDidLoad()
    // set initial location in Honolulu
    let initialLocation = CLLocation(latitude: 21.282778, longitude: -157.829444)
    centerMapOnLocation(location: initialLocation)
    //MapView.delegate = self
    // show artwork on map
    let artwork = Artwork(title: "King David Kalakaua",
                          locationName: "Waikiki Gateway Park",
                          discipline: "Sculpture",
                          coordinate: CLLocationCoordinate2D(latitude: 21.283921, longitude: -157.831661))
    MapView.addAnnotation(artwork)
}

let regionRadius: CLLocationDistance = 1000
func centerMapOnLocation(location: CLLocation) {
    let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate,
                                                              regionRadius, regionRadius)
    MapView.setRegion(coordinateRegion, animated: true)
   }
}

And my Artwork Swift File looks like this:

import Foundation
import MapKit
import Contacts

class Artwork: NSObject, MKAnnotation {
let title: String?
let locationName: String
let discipline: String
let coordinate: CLLocationCoordinate2D

init(title: String, locationName: String, discipline: String, coordinate: CLLocationCoordinate2D) {
    self.title = title
    self.locationName = locationName
    self.discipline = discipline
    self.coordinate = coordinate

    super.init()
}

var subtitle: String? {
    return locationName
}

// Annotation right callout accessory opens this mapItem in Maps app
func mapItem() -> MKMapItem {
    let addressDict = [CNPostalAddressStreetKey: subtitle!]
    let placemark = MKPlacemark(coordinate: coordinate, addressDictionary: addressDict)
    let mapItem = MKMapItem(placemark: placemark)
    mapItem.name = title
    return mapItem
  }
}

When i press the button, is doesn't work. Where is my mistake? Hope you can help me. I follow these Tutorial for the MapKit:

https://www.raywenderlich.com/548-mapkit-tutorial-getting-started

Thanks for your answer

Niklas
  • 1,638
  • 4
  • 19
  • 48
  • It appears you have yet to implement the `MKMapViewDelegate` extension as outlined in the tutorial. – RLoniello Aug 25 '18 at 23:16
  • yes, thats right... but when i use the Extension, how can i use my IBAction-Button!? – Niklas Aug 26 '18 at 05:30
  • The tutorial states `detail disclosure info button on the right side but tapping that button doesn’t do anything yet.` [you can implement your own button and action](https://stackoverflow.com/a/40380171/1361672) Good Luck and happy coding! – RLoniello Aug 26 '18 at 05:37
  • thanks for your answer, but this isn‘t helpfully for me, sorry. I don‘t want to use the Artwork View for open the Maps App. I want to use an overlay button in my ViewController... That's why I asked the question. Hope you can help me – Niklas Aug 26 '18 at 07:01

0 Answers0