reza_khalafi
reza_khalafi

Reputation: 6554

How to make a route in google map with url and parameters between origin and destination?

I want to open a url in web and automatically make a route between two points. I can't find correct url and params in google map document.

Upvotes: 0

Views: 3861

Answers (2)

Bijender Singh Shekhawat
Bijender Singh Shekhawat

Reputation: 4524

And for Swift 4 and swift 5

let apiKey = "https://www.google.com/maps/dir/" + userCurrentLocation.coordinate.latitude + "," + userCurrentLocation.coordinate.longitude + "/" + nextCurrentLocation.coordinate.latitude + "," + nextCurrentLocation.coordinate.longitude
                
    var url = URL(string: apikey)

For objective c use

   NSString *apikey = [NSString stringWithFormat:@"https://www.google.co.in/maps/dir/%f,%f/%f,%f",userCurrentLocation.coordinate.latitude,userCurrentLocation.coordinate.longitude,nextCurrentLocation.coordinate.latitude,nextCurrentLocation.coordinate.longitude;

NSURL *url = [NSURL URLWithString:apikey];

[[UIApplication sharedApplication] openURL:url];

Upvotes: 1

reza_khalafi
reza_khalafi

Reputation: 6554

You can do it like this in Swift 4 :

        let url = URL(string: "https://maps.google.com")
        if (UIApplication.shared.canOpenURL(url!))
        {
            let originLat:String = String(locationManager.location!.coordinate.latitude)
            let originLong:String = String(locationManager.location!.coordinate.longitude)
            let destLat:String = String(locationObj.latitude)
            let destLong:String = String(locationObj.longitude)

            let openUrlString:String = String(format:"https://www.google.com/maps/dir/?api=1&origin=%@,%@&destination=%@,%@&travelmode=driving",originLat,originLong,destLat,destLong)

            print("openUrlString is : \(openUrlString)")


            let openUrl = URL(string: openUrlString)
            UIApplication.shared.open(openUrl!, options: [:]) { (res:Bool) in

            }
        }

Upvotes: 1

Related Questions