1

I have been trying to get directions and routes with MKMapView without using Maps or google maps app. I want to use it in my app with opening up other apps. Can someone help?

Here is what I have but I want to change it so I can get the direction and routing on my app. I have Swift 4 and Xcode 9!

//Getting direction of location
    @objc func getDirections(){
        if let selectedPin = selectedPin {
            let mapItem = MKMapItem(placemark: selectedPin)
            let launchOptions = [MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving]
            mapItem.openInMaps(launchOptions: launchOptions)
        }
    }


extension ViewController : MKMapViewDelegate {
    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?{
        if annotation is MKUserLocation {
            //return nil so map view draws "blue dot" for standard user location
            return nil
        }
        let reuseId = "pin"
        var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView?.pinTintColor = UIColor.red
        pinView?.canShowCallout = true
        let smallSquare = CGSize(width: 30, height: 30)
        let button = UIButton(frame: CGRect(origin: CGPoint.zero, size: smallSquare))
        button.setBackgroundImage(UIImage(named: "Car"), for: .normal)
        button.addTarget(self, action: #selector(getDirections), for: .touchUpInside)

        pinView?.leftCalloutAccessoryView = button
        return pinView
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Azerswag
  • 91
  • 1
  • 10

1 Answers1

1

I have been trying to get directions and routes with MKMapView without using Maps or google maps app. I want to use it in my app with opening up other apps. Can someone help?

You can't start a navigation inside your apps. What you can do is drawing a path between two points and get information like the distance or expected driving/walking time.

Search for Distance between two points MapKitand you will find a bunch of results. For example have a look here, here or this swift 4 soultion

kuzdu
  • 7,124
  • 1
  • 51
  • 69