Как сделать маршрут в карте Google с URL и параметрами между пунктом отправления и пунктом назначения? - PullRequest
0 голосов
/ 30 июня 2018

Я хочу открыть URL в сети и автоматически проложить маршрут между двумя точками. Я не могу найти правильный URL и параметры в Google Map документе.

Ответы [ 2 ]

0 голосов
/ 30 июня 2018

А для Свифта 3 и Свифта 4

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)

Для цели c использовать

   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];
0 голосов
/ 30 июня 2018

Вы можете сделать это следующим образом: 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

            }
        }
...