如何在google map中使用起点和终点之间的网址和参数制作路线?

时间:2018-06-30 08:41:36

标签: ios google-maps routes direction google-maps-urls

我想在网络中打开一个 URL ,并自动在两点之间建立路线。我在 google map 文档中找不到正确的网址和参数。

2 个答案:

答案 0 :(得分:1)

您可以在 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

            }
        }

答案 1 :(得分:0)

对于Swift 3和Swift 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];