如何获得准确的地图路线

时间:2017-07-18 21:16:35

标签: ios swift google-maps-sdk-ios google-direction

我正在尝试使用Google Directions API构建路线。它返回一个航点点阵列,但是,航点的数量不足以构建一条平滑的航线,结果我收到了非常不准确的路线,这对于运输应用来说是非常重要和不合适的。我也试过HERE地图,结果差不多。

是否有其他服务可以构建更准确的路线,或适用于Google Directions API的任何解决方案?

这是功能:

public func getWaypoints(startLocation: [Double]!, endLocation: [Double]!, mode:String? = "walking", lang:String? = "en") -> [Dictionary<String,Any>] {

    var resultedArray:[Dictionary<String,Any>] = []
    let routeGM = Just.get("https://maps.googleapis.com/maps/api/directions/json?", params: ["origin":"\(startLocation[0]),\(startLocation[1])", "destination": "\(endLocation[0]),\(endLocation[1])", "mode": mode!, "key":self.KEY, "language": lang!])

    if let _ = routeGM.error {
    } else {
        do {
            if let data = jsonToNSData(routeGM.json! as AnyObject), let jsonData = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? NSDictionary {
                let status = jsonData["status"] as! String
                if(status == "OK") {
                    let results = jsonData["routes"] as! Array<Dictionary<String, AnyObject>>
                    let legs = results[0]["legs"] as! Array<Dictionary<String, AnyObject>>

                    let steps = legs[0]["steps"] as! Array<Dictionary<String, AnyObject>>

                    for i in 0...steps.count-1 {
                        let item = steps[i]
                        let start = item["start_location"] as! Dictionary<String,Any>
                        let end = item["end_location"] as! Dictionary<String,Any>
                        resultedArray.append(["start_location_lat": start["lat"]!,"start_location_lng": start["lng"]!,"end_location_lat": end["lat"]!,"end_location_lng": end["lng"]!])
                    }
                }
                else {
                    print("not found")
                }
            }
        } catch {
            print(error)
        }
    }
    return resultedArray
}

通话功能:

func buildRoute(startCoord:[Double]!, endCoord:[Double]!) {
    DispatchQueue.global(qos: .background).async {
        let route:[Dictionary<String,Any>] = self.GMSRequest.getWaypoints(startLocation: startCoord,endLocation: endCoord, mode: "driving")
        DispatchQueue.main.async {
            let path = GMSMutablePath()
            for item in route {
                path.add(CLLocationCoordinate2D(latitude: item["start_location_lat"]! as! CLLocationDegrees, longitude: item["start_location_lng"]! as! CLLocationDegrees))
                path.add(CLLocationCoordinate2D(latitude: item["end_location_lat"]! as! CLLocationDegrees, longitude: item["end_location_lng"]! as! CLLocationDegrees))
            }
            // Create the polyline, and assign it to the map.
            self.polyline.path = path
            self.polyline.strokeColor = Styles.colorWithHexString("#3768CD")
            self.polyline.strokeWidth = 3.0
            self.polyline.geodesic = true
            self.polyline.map = self.mapView
        }
    }
}

Route built based on waypoints Route built based on waypoints

0 个答案:

没有答案