Ios 获取2点纬度之间的旅行时间,长的Swift谷歌api

Ios 获取2点纬度之间的旅行时间,长的Swift谷歌api,ios,swift,Ios,Swift,我使用谷歌地图和谷歌地点,我想用lat和long来计算两点之间的旅行时间,我可以用android部分来计算,但在swift中,我找不到任何地方,我唯一能找到的是以公里为单位的距离 这是我的代码,在这里我可以得到Lat和long func placePicker(_ viewController: GMSPlacePickerViewController, didPick place: GMSPlace) { // Dismiss the place picker, as it c

我使用谷歌地图和谷歌地点,我想用lat和long来计算两点之间的旅行时间,我可以用android部分来计算,但在swift中,我找不到任何地方,我唯一能找到的是以公里为单位的距离

这是我的代码,在这里我可以得到Lat和long

func placePicker(_ viewController: GMSPlacePickerViewController, didPick place: GMSPlace) {
        // Dismiss the place picker, as it cannot dismiss itself.
        viewController.dismiss(animated: true, completion: nil)

        self.FullAddress = (place.formattedAddress?.components(separatedBy: ", ")
            .joined(separator: ","))!                        

        self.LatAuto = place.coordinate.latitude 
        self.LonAuto = place.coordinate.longitude 

         let location = CLLocation(latitude: self.LatAuto, longitude: self.LonAuto)

        self.fetchCityAndCountry(from: location) {
            city, country, error in
            guard let city = city, let country = country, error == nil else { return }
            print(city + ", " + country)

            self.City = city;
        }

        self.showLocation(LatSend:place.coordinate.latitude,LongSend: place.coordinate.longitude,FullAddSend:self.FullAddress)        
    } 

2:当使用placePicker时,如果地址是一个“否”点,则在用户选择位置后,我会出现错误,但在android版本中,我没有此错误,即使我对这两个位置都使用google API

使用方向google API获取距离:

let directionURL = "https://maps.googleapis.com/maps/api/directions/json?origin=\(sourceLat),\(sourceLng)&destination=\(destinationLat),\(destinationLng)"

Alamofire.request(directionURL, method: .post, params: [:], encoding: JSONEncoding.default, headers: nil).downloadProgress(queue: DispatchQueue.global(qos: .utility)){
                    progress in
                    print("Progress: \(progress.fractionCompleted)")
}
.responseJSON {
    response in
        if response.result.isSuccess {

          let JsonResponse = JSON(response.result.value!)
          let routes = JsonResponse["routes"].arrayValue
              for route in routes
              {

                 let duration = route["legs"][0]["duration"]["text"].stringValue

                  let distance = route["legs"][0]["distance"]["text"].stringValue
                }

           }
           if response.result.isFailure {
              // Show error 
           }
}
您将获得以下json:

{
   "geocoded_waypoints" : [
      {
         "geocoder_status" : "OK",
         "place_id" : "ChIJRVY_etDX3IARGYLVpoq7f68",
         "types" : [
            "bus_station",
            "transit_station",
            "point_of_interest",
            "establishment"
         ]
      },
      {
         "geocoder_status" : "OK",
         "partial_match" : true,
         "place_id" : "ChIJp2Mn4E2-woARQS2FILlxUzk",
         "types" : [ "route" ]
      }
   ],
   "routes" : [
      {
         "bounds" : {
            "northeast" : {
               "lat" : 34.1330949,
               "lng" : -117.9143879
            },
            "southwest" : {
               "lat" : 33.8068768,
               "lng" : -118.3527671
            }
         },
         "copyrights" : "Map data ©2016 Google",
         "legs" : [
            {
               "distance" : {
                  "text" : "35.9 mi",
                  "value" : 57824
               },
               "duration" : {
                  "text" : "51 mins",
                  "value" : 3062
               },
               "end_address" : "Universal Studios Blvd, Los Angeles, CA 90068, USA",
               "end_location" : {
                  "lat" : 34.1330949,
                  "lng" : -118.3524442
               },
               "start_address" : "Disneyland (Harbor Blvd.), S Harbor Blvd, Anaheim, CA 92802, USA",
               "start_location" : {
                  "lat" : 33.8098177,
                  "lng" : -117.9154353
               }}],
         "summary" : "I-5 N and US-101 N",
         "warnings" : [],
         "waypoint_order" : []
      }
   ],
   "status" : "OK"
}

期间你的意思是写距离吗?代码与你的问题有什么关系?你的问题毫无意义。期间你是说旅行时间吗?距离?然后你说“膨胀公里”。那是什么?以公里为单位的距离?@Duncan。我编辑了我的问题对不起我的英语关于你正在使用的库的链接如何?@duncac你是说placePicker?非常感谢,这看起来很好,将尝试返回到UPLASE检查未解析标识符“JsonResponse”在let routes=JsonResponse[“routes”]中的更新使用.arrayValue@zezoalharazi请检查未解析标识符“JSON”的使用情况