Google Maps for iOS, swift - How to show entire polyline between markers?

10,849

Solution 1

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))

Solution 2

Though the question has an accepted answer,

Still one point which has not been discussed.

GMSPolyLine has a property ".path" which can used to bound the map with the complete polyline itself.

mapView.animate(with: GMSCameraUpdate.fit(GMSCoordinateBounds(path: self.polyLineObject.path!), withPadding: 10))
  • mapView is an instance of GMSMapView()
  • polyLineObject is an instance of GMSPolyLine()

With this approach, It'll do the job for sure.

Coz it has Done for me..

Hope it Helps..

Solution 3

You can zoom the map according the route or line displayed in the mapview. So that your route is displayed with in the mapView bounds.

let bounds = GMSCoordinateBounds(path:path! )
self.mapView!.animate(with: GMSCameraUpdate.fit(bounds, withPadding: 50.0))

Here path is GMSPath of the route and mapView is GmsMapView.

Solution 4

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


func fitAllMarkers(_path: GMSPath) {
        var bounds = GMSCoordinateBounds()
        for index in 1..._path.count() {
            bounds = bounds.includingCoordinate(_path.coordinate(at: index))
        }
        mapView.animate(with: GMSCameraUpdate.fit(bounds))
    }
Share:
10,849
lifewithelliott
Author by

lifewithelliott

Updated on July 27, 2022

Comments

  • lifewithelliott
    lifewithelliott almost 2 years

    I am trying to fit a polyline in the google map view. The polyline was acquired through overview_polyline in the google maps directions api.

    Wondering how I would be able to convert an encoded polyline into something that can be worked with. I need to fit the polyline in the map view. All i have found out to do is fit the bounds to show all markers but not showcase the entire polyline.

    func fitAllMarkers()
    {
    
        var bounds = GMSCoordinateBounds()
    
        for marker in markers
        {
            bounds = bounds.includingCoordinate(marker.position)
        }
    
        googleMapView.animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds))
    
    }
    
  • lifewithelliott
    lifewithelliott about 8 years
    I appreciate the update greatly! Code works perfectly. I have one question though, Do you know how to possibly size the polyline to fit within a certain region of the screen. Example: Zoom into the polyline but the polyline is centered with 20 pts of space from the top and bottom of the screen? Thanks Again! :)
  • Manuel BM
    Manuel BM about 8 years
    Hello! I needed to do the same thing but for now I could only resize the mapView's constraints to fit the region of the screen I wanted it to cover, then applied this "fitBounds" function. (Resizing it again to full screen after fittingBounds does not work).
  • 21.kaw
    21.kaw almost 5 years
    for swift 4, changed to bounds.includingCoordinate(path.coordinate(at: index))
  • Yash Bedi
    Yash Bedi over 3 years
    glad I could help.!