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.
Asked
Active
Viewed 3,509 times
0
2 Answers
1
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];

Bijender Singh Shekhawat
- 3,934
- 2
- 30
- 36
0
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
}
}

reza_khalafi
- 6,230
- 7
- 56
- 82