Not Getting the "MAP Route" - ios

Why I am not Getting the "MAP Route", whats the issue with this code. Or, Kindly provide me the code for "Getting Route on Map"
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView,
calloutAccessoryControlTapped control: UIControl) {
let selectedLoc = view.annotation
print("Annotation '\(String(describing: selectedLoc?.title!))' has been selected")
let currentLocMapItem = MKMapItem.forCurrentLocation()
let selectedPlacemark = MKPlacemark(coordinate: (selectedLoc?.coordinate)!, addressDictionary: nil)
let selectedMapItem = MKMapItem(placemark: selectedPlacemark)
let mapItems = [selectedMapItem, currentLocMapItem]
let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
MKMapItem.openMaps(with: mapItems, launchOptions:launchOptions)
}

I had added this method to create a map route between 2 MKMapItem
func createRouteTo(fromMapItem: MKMapItem,selectedMapItem: MKMapItem) {
if(CLLocationCoordinate2DIsValid(self.mapview.userLocation.coordinate) && CLLocationCoordinate2DIsValid(selectedMapItem.placemark.coordinate))
{
self.showLoading(message: "Creando Ruta...")
let directionRequest = MKDirectionsRequest()
directionRequest.source = MKMapItem(placemark: MKPlacemark(coordinate: fromMapItem.placemark.coordinate, addressDictionary: nil))
directionRequest.destination = MKMapItem(placemark: MKPlacemark(coordinate: selectedMapItem.placemark.coordinate, addressDictionary: nil))
directionRequest.transportType = .automobile
directionRequest.requestsAlternateRoutes = false
// Calculate the direction
let directions = MKDirections(request: directionRequest)
directions.calculate { [unowned self] (directionResponse, error) in
self.hideLoading(withDelay: 0.1)
guard let response = directionResponse else {
if let error = error {
print("Error: \(error)")
let alert = UIAlertController.init(title:"Error Creando Ruta",
message:error.localizedDescription, preferredStyle: .alert)
alert.addAction(UIAlertAction.init(title: "Aceptar", style: .destructive, handler:nil))
self.present(alert, animated:true , completion:nil)
}
return
}
if(self.currentRouteOverlay != nil)
{
self.mapview.remove(self.currentRouteOverlay!)
}
let route = response.routes[0]
self.currentRouteOverlay = route.polyline
self.mapview.add(route.polyline, level: .aboveRoads)
let rect = route.polyline.boundingMapRect
self.mapview.setVisibleMapRect(rect, edgePadding: UIEdgeInsetsMake(15, 15, 15, 15), animated: true)
}
}
}
this is your method modified
// This method will be called when we will tap on the annotation for Details.Then it will open in new View and make a route from current location to selected location.
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView,
calloutAccessoryControlTapped control: UIControl) {
let selectedLoc = view.annotation
print("Annotation '\(String(describing: selectedLoc?.title!))' has been selected")
let currentLocMapItem = MKMapItem(placemark: MKPlacemark(coordinate: mapView.userLocation.coordinate))
let selectedPlacemark = MKPlacemark(coordinate: (selectedLoc?.coordinate)!, addressDictionary: nil)
let selectedMapItem = MKMapItem(placemark: selectedPlacemark)
self.createRouteTo(fromMapItem:currentLocMapItem, selectedMapItem: selectedMapItem)
}
full code here https://github.com/rmelian2014/MapRouteQuestion

Related

draw polyline between two latitude and longitude

I pass two lat and long for source and destination but not draw poly line between two point.always get something wrong error .below code I pass my current location in source coordinate and travel location pass in destCoordinates. I want draw poly line between sourceCoordinates to destCoordinates..
if(CLLocationManager.locationServicesEnabled()){
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
//let sourceCoordinates = locationManager.location?.coordinate
let sourceCoordinates = CLLocationCoordinate2DMake(19.115950,72.856448)
let destCoordinates = CLLocationCoordinate2DMake(19.119670,72.853616)
let sourcePlacemark = MKPlacemark(coordinate: sourceCoordinates)
let destPlacemark = MKPlacemark(coordinate: destCoordinates)
let sourceItem = MKMapItem(placemark: sourcePlacemark)
let destItem = MKMapItem(placemark: destPlacemark)
let directionRequest = MKDirectionsRequest()
directionRequest.source=sourceItem
directionRequest.destination=destItem
directionRequest.transportType = .walking
let directions = MKDirections(request: directionRequest)
directions.calculate(completionHandler: {
response,error in
guard let response = response else{
if let error = error {
print("something wrong")
}
return
}
let route = response.routes[0]
self.upcoming_info_map.add(route.polyline,level: .aboveRoads)
let rekt = route.polyline.boundingMapRect
self.upcoming_info_map.setRegion(MKCoordinateRegionForMapRect(rekt), animated: true)
})
for draw polyline i use this function
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolygonRenderer(overlay:overlay)
renderer.strokeColor = UIColor.blue
renderer.lineWidth = 5.0
return renderer
}
use this method.takes list of locations and draw polyline
func addPolyLineToMap(googlemaplist: [CLLocation?]){
var coordinates = googlemaplist.map({ (location: CLLocation!) -> CLLocationCoordinate2D in
return location.coordinate
})
print("locatios count")
print(googlemaplist.count)
var polyline = MKPolyline(coordinates: &coordinates, count: googlemaplist.count)
DispatchQueue.main.async {
self.MapKit.add(polyline)
}
}

MKDirections : Directions Not Available

I am creating an app like an UBER and i am using MapKit ,i want to display route between two locations and for this i have use following code
viewMap.delegate = self
let sourceLocation = CLLocationCoordinate2D(latitude: sourceLatitude, longitude: sourceLongitude)
let destinationLocation = CLLocationCoordinate2D(latitude: destinationLatitude, longitude: destinationLongitude)
let sourcePlacemark = MKPlacemark(coordinate: sourceLocation, addressDictionary: nil)
let destinationPlacemark = MKPlacemark(coordinate: destinationLocation, addressDictionary: nil)
let sourceMapItem = MKMapItem(placemark: sourcePlacemark)
let destinationMapItem = MKMapItem(placemark: destinationPlacemark)
let sourceAnnotation = MKPointAnnotation()
sourceAnnotation.title = strSource
if let location = sourcePlacemark.location {
sourceAnnotation.coordinate = location.coordinate
}
let destinationAnnotation = MKPointAnnotation()
destinationAnnotation.title = strDestination
if let location = destinationPlacemark.location {
destinationAnnotation.coordinate = location.coordinate
}
self.viewMap.showAnnotations([sourceAnnotation,destinationAnnotation], animated: true )
let directionRequest = MKDirectionsRequest()
directionRequest.source = sourceMapItem
directionRequest.destination = destinationMapItem
directionRequest.transportType = .automobile
let directions = MKDirections(request: directionRequest)
directions.calculate {
(response, error) -> Void in
guard let response = response else {
if let error = error {
print("Route Error: \(error)")
}
return
}
let route = response.routes[0]
self.viewMap.add((route.polyline), level: MKOverlayLevel.aboveRoads)
let rect = route.polyline.boundingMapRect
self.viewMap.setRegion(MKCoordinateRegionForMapRect(rect), animated: true)
}
extension HomeVC: MKMapViewDelegate
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(overlay: overlay)
renderer.strokeColor = #colorLiteral(red: 0.2392156869, green: 0.6745098233, blue: 0.9686274529, alpha: 1)
renderer.lineWidth = 2.0
return renderer
}
I have check all the possible solution and i am also getting latitude and longitude of source and destination Locations but i still its not working and i am getting following error.
Please help me with this!
Apple maps directions is available to specific countries only. Please check full list here. Apple Map Availability list (Check in Maps: Directions section). You should look into Google Map SDK in your app is targeting countries not listed in above link.
This works for me and it draws the destination in the MK Map, also it focuses to the destination
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
let request = MKDirectionsRequest()
request.source = MKMapItem.forCurrentLocation()
request.destination = MKMapItem(placemark: MKPlacemark(coordinate: (view.annotation?.coordinate)!))
request.requestsAlternateRoutes = true
request.requestsAlternateRoutes = true
request.transportType = .walking
let directions = MKDirections(request: request)
directions.calculate { [unowned self] response, error in
guard let unwrappedResponse = response else { return }
for route in unwrappedResponse.routes {
self.myMapView.add(route.polyline) self.myMapView.setVisibleMapRect(route.polyline.boundingMapRect, animated: true)
return
}
}
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let polyLine = overlay
let polyLineRenderer = MKPolylineRenderer(overlay: polyLine)
polyLineRenderer.strokeColor = UIColor.blue
polyLineRenderer.lineWidth = 2.0
return polyLineRenderer
}
this works when you click on a MKAnnotationView, if you want to draw from another code you just have to cut the code to your function and change the line
request.destination = MKMapItem(placemark: MKPlacemark(coordinate: (view.annotation?.coordinate)!))
to
request.destination = MKMapItem(placemark: MKPlacemark(coordinate: (CLLocationCoordinate2D(/*Your Location*/))!))

MapKit Direction to Selected annotation

Trying to get direction to selected annotation on the map from the current location of the User but not sure how to catch selected annotation(when user selecting one of them) and show direction.Checked different topics on the Stack but there is not a lot of fresh information about how catch selected annotation and show direction for user location.Please advise.
import UIKit
import MapKit
import CoreLocation
class NavigationVC: UIViewController {
#IBOutlet weak var mapView: MKMapView!
let manager = CLLocationManager()
let request = MKLocalSearchRequest()
override func viewDidLoad() {
super.viewDidLoad()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
}
}
//MARK: CONFIGURATION OF MAPVIEW
extension NavigationVC: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations
locations: [CLLocation]) {
let location = locations[0]
let span: MKCoordinateSpan = MKCoordinateSpanMake(0.1, 0.1 )
let userLocation:CLLocationCoordinate2D =
CLLocationCoordinate2DMake(location.coordinate.latitude,
location.coordinate.longitude)
let region: MKCoordinateRegion =
MKCoordinateRegionMake(userLocation, span)
mapView.setRegion(region, animated: true)
self.mapView.showsUserLocation = true
request.naturalLanguageQuery = "Currency exchange"
request.region = mapView.region
let activeSearch = MKLocalSearch(request: request)
activeSearch.start { (response, error) in
guard let response = response else {
return
}
for item in response.mapItems {
let annotation = MKPointAnnotation()
annotation.coordinate = item.placemark.coordinate
annotation.title = item.name
DispatchQueue.main.async {
self.mapView.addAnnotation(annotation)
}
}
}
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) ->
MKOverlayRenderer {
let polylineRenderer = MKPolylineRenderer(overlay: overlay)
polylineRenderer.strokeColor = UIColor.blue
polylineRenderer.fillColor = UIColor.red
polylineRenderer.lineWidth = 2
return polylineRenderer
}
}
To get the path on the map from your position to the annotation follow this code:
annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: CLLocationDegrees(lat),longitude: CLLocationDegrees(lon))
self.map?.addAnnotation(annotation)
var route : MKRoute? = nil
DispatchQueue.global(qos: .userInitiated).async { [weak self] in
let directionRequest = MKDirectionsRequest()
directionRequest.source = MKMapItem.forCurrentLocation();
directionRequest.destination = MKMapItem(placemark: MKPlacemark(coordinate:(self?.annotation.coordinate)!))
directionRequest.transportType = .automobile
let directions = MKDirections(request: directionRequest)
directions.calculate {
(response, error) -> Void in
guard let response = response else {
if let error = error {
print("Error: \(error)")
}
return
}
route = response.routes[0]
if let percorso = route{
DispatchQueue.main.async { [weak self] in
self?.map?.add((percorso.polyline), level: MKOverlayLevel.aboveRoads)
}
}
}
}
You have only to select the correct annotation, maybe try to register a touch listener on the annotation.
Use didSelect delegate method.
e.g.
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
guard let annotation = view.annotation else {
return
}
let directionRequest = MKDirectionsRequest()
directionRequest.source = MKMapItem.forCurrentLocation()
directionRequest.destination = MKMapItem(placemark: MKPlacemark(coordinate: annotation.coordinate))
directionRequest.transportType = .automobile
let directions = MKDirections(request: directionRequest)
directions.calculate {
(response, error) -> Void in
guard let response = response else {
if let error = error {
print("Error: \(error)")
}
return
}
if !response.routes.isEmpty {
let route = response.routes[0]
DispatchQueue.main.async { [weak self] in
self?.mapView.add(route.polyline)
}
}
}
}
Don't forget guard to prevent crash.
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) ->
MKOverlayRenderer {
guard overlay is MKPolyline else {
return MKPolylineRenderer()
}
...
}
Good luck!

Swift 3 - UIView loads every time when adding a location coordinate

I am using firebase as my backend. In this application I have used mapkit and firebase. I want to track the location of user. But when I am doing the location of user is sucessfully uploaded to firebase but in my application the view is reloaded.
my code:-
#IBOutlet weak var map:MKMapView!
let locationManager = CLLocationManager()
var lat = [Double]()
var lon = [Double]()
var ref:DatabaseReference!
var uid = ""
override func viewDidLoad() {
super.viewDidLoad()
map.delegate = self
locationManager.delegate = self
ref = Database.database().reference().child("users").child(uid)
ref.child("locations").queryOrderedByKey().observe(.childAdded, with: { (snapshot) in
print("func snapshot \(snapshot)")
let dict = snapshot.value as! [String:Double]
let lat = dict["latitude"]
let lon = dict["longitude"]
print("latidue = \(lat!)")
print("longitude = \(lon!)")
self.lat.append(lat!)
self.lon.append(lon!)
})
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let location = CLLocationCoordinate2D(latitude: lat.first!,longitude: lon.first!)
let destinationLocation = CLLocationCoordinate2D(latitude: lat.last!, longitude:lon.last!)
let sourcePlacemark = MKPlacemark(coordinate: location, addressDictionary: nil)
let destinationPlacemark = MKPlacemark(coordinate: destinationLocation, addressDictionary: nil)
let sourceMapItem = MKMapItem(placemark: sourcePlacemark)
let destinationMapItem = MKMapItem(placemark: destinationPlacemark)
let sourceAnnotation = MKPointAnnotation()
if let location = sourcePlacemark.location {
sourceAnnotation.coordinate = location.coordinate
}
let destinationAnnotation = MKPointAnnotation()
if let location = destinationPlacemark.location {
destinationAnnotation.coordinate = location.coordinate
}
let directionRequest = MKDirectionsRequest()
directionRequest.source = sourceMapItem
directionRequest.destination = destinationMapItem
directionRequest.transportType = .automobile
let directions = MKDirections(request: directionRequest)
directions.calculate {
(response, error) -> Void in
guard let response = response else {
if let error = error {
print("Error: \(error)")
}
return
}
let route = response.routes[0]
self.map.add((route.polyline), level: MKOverlayLevel.aboveRoads)
let rect = route.polyline.boundingMapRect
self.map.setRegion(MKCoordinateRegionForMapRect(rect), animated: true)
}
let span = MKCoordinateSpanMake(0.05, 0.05)
let region = MKCoordinateRegion(center: location, span: span)
map.setRegion(region, animated: true)
let annotation = MKPointAnnotation()
annotation.coordinate = location
map.addAnnotation(annotation)
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(overlay: overlay)
renderer.strokeColor = UIColor.red
renderer.lineWidth = 4.0
return renderer
}
I don't want to reload my whole view just want to render my mapview whenever new cordinate is uploaded.
Remove code from viewDidAppear and put it inside the observe childAdded callback

How to open maps App programmatically with coordinates in swift?

I have latitude and longitude that I want to open into my map application. I tried this code from HERE.
func goToMap(){
var lat1 : NSString = self.venueLat
var lng1 : NSString = self.venueLng
var latitude:CLLocationDegrees = lat1.doubleValue
var longitude:CLLocationDegrees = lng1.doubleValue
var coordinate = CLLocationCoordinate2DMake(latitude, longitude)
var placemark : MKPlacemark = MKPlacemark(coordinate: coordinate, addressDictionary:nil)
var mapItem:MKMapItem = MKMapItem(placemark: placemark)
mapItem.name = "Target location"
let launchOptions:NSDictionary = NSDictionary(object: MKLaunchOptionsDirectionsModeDriving, forKey: MKLaunchOptionsDirectionsModeKey)
var currentLocationMapItem:MKMapItem = MKMapItem.mapItemForCurrentLocation()
MKMapItem.openMapsWithItems([currentLocationMapItem, mapItem], launchOptions: launchOptions)
}
This function successfully open maps but it doesn't show any pin. Also it shows user location which I don't want. I only want a pin on the map for the provided latitude and longitude.
This code is working fine for me.
func openMapForPlace() {
let lat1 : NSString = self.venueLat
let lng1 : NSString = self.venueLng
let latitude:CLLocationDegrees = lat1.doubleValue
let longitude:CLLocationDegrees = lng1.doubleValue
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 = "\(self.venueName)"
mapItem.openInMapsWithLaunchOptions(options)
}
For swift 3.0:
import UIKit
import MapKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
openMapForPlace()
}
func openMapForPlace() {
let latitude: CLLocationDegrees = 37.2
let longitude: CLLocationDegrees = 22.9
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 = "Place Name"
mapItem.openInMaps(launchOptions: options)
}
}
Swift 5:
let latitude: CLLocationDegrees = Double(K.latitude)!
let longitude: CLLocationDegrees = Double(K.longitude)!
let regionDistance:CLLocationDistance = 10000
let coordinates = CLLocationCoordinate2DMake(latitude, longitude)
let regionSpan = MKCoordinateRegion(center: coordinates, latitudinalMeters: regionDistance, longitudinalMeters: 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 = K.companyName
mapItem.openInMaps(launchOptions: options)
If you just want to give the user driving directions, here's the latest Swift syntax in its simplest form:
let coordinate = CLLocationCoordinate2DMake(theLatitude,theLongitude)
let mapItem = MKMapItem(placemark: MKPlacemark(coordinate: coordinate, addressDictionary:nil))
mapItem.name = "Target location"
mapItem.openInMaps(launchOptions: [MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving])
You could call class function of MKMapItem passing items there, it uses only first and last for source / destination appropriately, if you want pass more than two items.
Swift 5, 4
let source = MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: lat, longitude: lng)))
source.name = "Source"
let destination = MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: lat, longitude: lng)))
destination.name = "Destination"
MKMapItem.openMaps(
with: [source, destination],
launchOptions: [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
)
or using extension:
extension MKMapItem {
convenience init(coordinate: CLLocationCoordinate2D, name: String) {
self.init(placemark: .init(coordinate: coordinate))
self.name = name
}
}
let source = MKMapItem(coordinate: .init(latitude: lat, longitude: lng), name: "Source")
let destination = MKMapItem(coordinate: .init(latitude: lat, longitude: lng), name: "Destination")
MKMapItem.openMaps(
with: [source, destination],
launchOptions: [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
)
The MKMapItem approach above works great if you want granular control over the information that is displayed in Maps.
Otherwise, the code below works great as well, :
// Open and show coordinate
let url = "http://maps.apple.com/maps?saddr=\(coord.latitude),\(coord.longitude)"
UIApplication.shared.openURL(URL(string:url)!)
// Navigate from one coordinate to another
let url = "http://maps.apple.com/maps?saddr=\(from.latitude),\(from.longitude)&daddr=\(to.latitude),\(to.longitude)"
UIApplication.shared.openURL(URL(string:url)!)
However, the code above does not let you to send in a custom name of the place. Instead, it will show the address.
The code above also lets you navigate from any source coordinate, which I don't know if you can do with the MKMapItem approach.
This works as a charm for me
let coordinate = CLLocationCoordinate2DMake(theLatitude, theLongitude)
let region = MKCoordinateRegionMake(coordinate, MKCoordinateSpanMake(0.01, 0.02))
let placemark = MKPlacemark(coordinate: coordinate, addressDictionary: nil)
let mapItem = MKMapItem(placemark: placemark)
let options = [
MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: region.center),
MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: region.span)]
mapItem.name = theLocationName
mapItem.openInMaps(launchOptions: options)
You can use below code to show PIN on lat, long in to Apple map.
let coordinates = CLLocationCoordinate2DMake(-37.848854,144.990295)
let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, 1000, 1000)
let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = “Desired place”
mapItem.openInMaps(launchOptions:[
MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center)
] as [String : Any])
If what you want is something simple without importing any framework you can just create a URL: https://maps.apple.com/?ll=\(latitude),\(longitude)
Is similar to #saniel-saidi response but this one opens just the map with location sent, not the navigation thing
Update to the practical Daniel Saidi answer. This example is for telling only the destination coordinates. Maps will get as origin the user current position.
let url = URL(string: "http://maps.apple.com/maps?saddr=&daddr=\(lat),\(lon)")
UIApplication.shared.open(url!)
I know all answers are complete but here I got an answer which is easier to copy paste & also gives the user options to routing with Apple Maps, Google Map & Waze.
Working with Swift 5+
https://stackoverflow.com/a/60930491/6449292
Might help someone...
The simplest way to open the Apple Maps app and show a pin on the custom location with a custom title is that:
let url = URL(string: "maps://?q=Title&ll=\(latitude),\(longitude)")!
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url)
}
Look here for more options:
https://developer.apple.com/library/archive/featuredarticles/iPhoneURLScheme_Reference/MapLinks/MapLinks.html
Swift 5 Solution:
Open Apple Map OR Google Map Programmatically
let actionSheet = UIAlertController(title: "Open Location", message: "Choose an app to open direction", preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Google Maps", style: .default, handler: { _ in
// Pass the coordinate inside this URL
self.openGoogleMap()
}))
actionSheet.addAction(UIAlertAction(title: "Apple Maps", style: .default, handler: { _ in
// Pass the coordinate that you want here
self.openAppleMap()
}))
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(actionSheet, animated: true, completion: nil)
//Function defination here
func openAppleMap(){
guard let lat = bookingData.gpsLatitude, let latDouble = Double(lat) else {return }
guard let long = bookingData.gpsLongitude, let longDouble = Double(long) else {return }
let coordinate = CLLocationCoordinate2DMake(latDouble,longDouble)
let mapItem = MKMapItem(placemark: MKPlacemark(coordinate: coordinate, addressDictionary: nil))
mapItem.name = "Destination"
mapItem.openInMaps(launchOptions: [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving])
}
func openGoogleMap() {
guard let lat = bookingData.gpsLatitude, let latDouble = Double(lat) else {return }
guard let long = bookingData.gpsLongitude, let longDouble = Double(long) else {return }
if (UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!)) { //if phone has an app
if let url = URL(string: "comgooglemaps-x-callback://?saddr=&daddr=\(latDouble),\(longDouble)&directionsmode=driving") {
UIApplication.shared.open(url, options: [:])
}}
else {
//Open in browser
if let urlDestination = URL.init(string: "https://www.google.co.in/maps/dir/?saddr=&daddr=\(latDouble),\(longDouble)&directionsmode=driving") {
UIApplication.shared.open(urlDestination)
}
}
}
Don't forget to write this in info.plist
<key>LSApplicationQueriesSchemes</key>
<array>
<string>comgooglemaps</string>
<string>comgooglemaps-x-callback</string>
</array>

Resources