获得2点lat之间的旅行时间,较长的Swift Google api

时间:2018-10-03 09:57:10

标签: ios swift

我使用google map和google place,我想使用lat和long查找2点之间的旅行时间,我可以在android部分中做到这一点,但很快我找不到任何地方,我唯一找到的就是距离(KM)。

这是我得到纬度和经度的代码

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

1 个答案:

答案 0 :(得分:1)

使用路线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"
}