Draw Polyline Using Google Maps in Custom View with Swift 3

Draw polyline using Google Maps in custom view with Swift 3

It works fine here. Make sure you're setting correct coordinates of GMSCameraPosition.

EDIT

To draw the route between two coordinate, use Google Maps Direction API

Something like :

    let origin = "\(37.778483),\(-122.513960)"
let destination = "\(37.706753),\(-122.418677)"
let url = "https://maps.googleapis.com/maps/api/directions/json?origin=\(origin)&destination=\(destination)&mode=driving&key=[YOUR-API-KEY]"

Alamofire.request(url).responseJSON { response in
let json = JSON(data: response.data!)
let routes = json["routes"].arrayValue

for route in routes
{
let routeOverviewPolyline = route["overview_polyline"].dictionary
let points = routeOverviewPolyline?["points"]?.stringValue
let path = GMSPath.init(fromEncodedPath: points!)

let polyline = GMSPolyline(path: path)
polyline.strokeColor = .black
polyline.strokeWidth = 10.0
polyline.map = mapViewX

}
}

For more info - Directions API Developer's Guide

Swift 3 draw a polyline with Google Maps during run

Try this

let path = GMSMutablePath()
//Change coordinates
path.add(CLLocationCoordinate2D(latitude: -33.85, longitude: 151.20))
path.add(CLLocationCoordinate2D(latitude: -33.70, longitude: 151.40))
path.add(CLLocationCoordinate2D(latitude: -33.73, longitude: 151.41))
let polyline = GMSPolyline(path: path)
polyline.strokeColor = UIColor.blue
polyline.strokeWidth = 3.0
polyline.map = mapView

SWIFT: google maps draw waypoint polyline

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..<path.count() {
self.add(path.coordinate(at: i))
}
}
}

}



Related Topics



Leave a reply



Submit