0

我想在 web 中打开一个url并自动在两点之间建立一条路线。我在谷歌地图文档中找不到正确的网址和参数。

4

2 回答 2

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

            }
        }
于 2018-06-30T10:23:19.730 回答
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];
于 2018-06-30T10:28:08.827 回答