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
let point3 = CLLocationCoordinate2D(latitude: Double(30.7173), longitude: Double(76.8329))
let point4 = CLLocationCoordinate2D(latitude: Double(30.6942), longitude: Double(76.8606))
let point5 = CLLocationCoordinate2D(latitude: Double(30.7465), longitude: Double(76.7872))
var arrOfWayPoints : NSMutableArray = NSMutableArray()
arrOfWayPoints.insert(point3, at: 0)
arrOfWayPoints.insert(point4, at: 1)
arrOfWayPoints.insert(point5, at: 2)
self.drawRouteWithWaypoint(positions: arrOfWayPoints as! [CLLocationCoordinate2D])
static var distance = Double()
func drawRouteWithWaypoint(positions:[CLLocationCoordinate2D]) {
LiveJob.getDotsToDrawRoute(positions: positions, completion: { path in
//self.route.countRouteDistance(p: path)
self.polyline.path = path
self.polyline.strokeColor = UIColor.blue
self.polyline.strokeWidth = 2.0
self.polyline.map = self.mapView
})
self.lblDistance.text = String(LiveJob.distance)
}
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,"mode" : "Transit","key" : "AIzaSyCtMHyxPEModWK8IgzBD96hQMFL-UCIjcY"]
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 newLeg = legs[0]
let distance = newLeg["distance"]
// LiveJob.distance = LiveJob.distance + distance!.doubleValue
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(path: GMSMutablePath(fromEncodedPath: points))
}
}
}
completion(fullPath)
}
}
}
}
}
})
}
}