How to draw a route between two markers in google maps (swift )?

P.Tb picture P.Tb · Jun 15, 2016 · Viewed 10.4k times · Source

I want to show direction between two markers that I had made. How can I do it? How can I show direction?

Here is my code that I used to make markers:

func mapView(mapView: GMSMapView, didLongPressAtCoordinate coordinate: CLLocationCoordinate2D) {
    if counterMarker < 2
    {
        counterMarker += 1
        let marker = GMSMarker(position: coordinate)
        marker.appearAnimation = kGMSMarkerAnimationPop

        marker.map = mapView
        marker.position.latitude = coordinate.latitude
        marker.position.longitude = coordinate.longitude

        print(marker.position.latitude)
        print(marker.position.longitude)

    }

And here is the code for deleting markers on click:

func mapView(mapView: GMSMapView, didTapMarker marker: GMSMarker) -> Bool {

        let alert = UIAlertController(title: "Alert", message: "Are you Sure for deleting ?!", preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "No", style: UIAlertActionStyle.Default) {
            UIAlertAction in
            NSLog("No Pressed")


            })
        alert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default) {
            UIAlertAction in
            NSLog("Yes Pressed")
            marker.map = nil
            self.counterMarker -= 1

        })

        self.presentViewController(alert, animated: true, completion: nil)

        return true
    }

And I like to show which marker is for destination and which one is for origin.

Answer

abielita picture abielita · Jun 16, 2016

Use Polylines which allows you to draw lines on the map. You'll need to specify its path by creating a corresponding GMSMutablePath object with two or more points. Each CLLocationCoordinate2D represents a point on the Earth's surface. Line segments are drawn between points according to the order in which you add them to the path.

Example:

let path = GMSMutablePath()
path.addCoordinate(CLLocationCoordinate2D(latitude: 37.36, longitude: -122.0))
path.addCoordinate(CLLocationCoordinate2D(latitude: 37.45, longitude: -122.0))
path.addCoordinate(CLLocationCoordinate2D(latitude: 37.45, longitude: -122.2))
path.addCoordinate(CLLocationCoordinate2D(latitude: 37.36, longitude: -122.2))
path.addCoordinate(CLLocationCoordinate2D(latitude: 37.36, longitude: -122.0))

let rectangle = GMSPolyline(path: path)
rectangle.map = mapView

Check these related links:

For Swift 2.0 Google Maps, to make your map view fit the polyline of the route you are drawing:

let path: GMSPath = GMSPath(fromEncodedPath: route)!
    routePolyline = GMSPolyline(path: path)
    routePolyline.map = mapView


    var bounds = GMSCoordinateBounds()

    for index in 1...path.count() {
        bounds = bounds.includingCoordinate(path.coordinateAtIndex(index))
    }

    mapView.animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds))

Hope this helps! :)