SWIFT: google maps draw waypoint polyline

前端 未结 4 1875
被撕碎了的回忆
被撕碎了的回忆 2021-01-07 09:12

Hi I wonder if there is a method to draw a waypoint between two or more markers in google maps iOS. I don\'t want to draw straight lines... but use just public roads. Here i

4条回答
  •  清酒与你
    2021-01-07 10:08

    To draw polylines between points two ore more you should use google map request read this link https://developers.google.com/maps/documentation/directions/intro#Waypoints in my case i did

        func drawRoute() {
        ServerCommunicator.getDotsToDrawRoute(positions: positions, completion: { path in
            self.route.countRouteDistance(p: path)
            self.polyline.path = path
            self.polyline.strokeColor = UserSession.tintColor
            self.polyline.strokeWidth = 4.0
            self.polyline.map = self._mapView
        })
    }
    

    and the part with request

    static func getDotsToDrawRoute(positions : [CLLocationCoordinate2D], completion: @escaping(_ path : GMSPath) -> Void) {
        if positions.count > 1 {
            let origin = positions.first
            let destination = positions.last
            var wayPoints = ""
            for point in positions {
                wayPoints = wayPoints.characters.count == 0 ? "\(point.latitude),\(point.longitude)" : "\(wayPoints)|\(point.latitude),\(point.longitude)"
            }
            let request = "https://maps.googleapis.com/maps/api/directions/json"
            let parameters : [String : String] = ["origin" : "\(origin!.latitude),\(origin!.longitude)", "destination" : "\(destination!.latitude),\(destination!.longitude)", "wayPoints" : wayPoints, "key" : googleAPI_KEY]
            Alamofire.request(request, method:.get, parameters : parameters).responseJSON(completionHandler: { response in
                guard let dictionary = response.result.value as? [String : AnyObject]
                    else {
                        return
                }
                if let routes = dictionary["routes"] as? [[String : AnyObject]] {
                    if routes.count > 0 {
                        var first = routes.first
                        if let legs = first!["legs"] as? [[String : AnyObject]] {
                            let fullPath : GMSMutablePath = GMSMutablePath()
                            for leg in legs {
                                if let steps = leg["steps"] as? [[String : AnyObject]] {
                                    for step in steps {
                                        if let polyline = step["polyline"] as? [String : AnyObject] {
                                            if let points = polyline["points"] as? String {
                                                fullPath.appendPath(GMSMutablePath(fromEncodedPath: points))
                                            }
                                        }
                                    }
                                    completion(path: fullPath)
                                }
                            }
                        }
                    }
                }
            })
        }
    }
    extension GMSMutablePath {
    
    func appendPath(path : GMSPath?) {
        if let path = path {
            for i in 0..

    }

提交回复
热议问题