Google Maps for iOS, swift - How do I show the entire polyline between markers?

I am trying to set a polyline as a google map. The polyline was obtained through overview_polyline in the google maps api directions.

I wonder how I can convert an encoded polyline to something I can work with. I need to set the polyline as a map. All I found was to fit the borders to show all the markers but not show the whole polyline.

func fitAllMarkers()
{

    var bounds = GMSCoordinateBounds()

    for marker in markers
    {
        bounds = bounds.includingCoordinate(marker.position)
    }

    googleMapView.animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds))

}
+5
source share
4 answers

For Swift 2.0 Google Maps, so that your map display matches the polyline of the route you draw:

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))
+9
source

,

, .

GMSPolyLine ".path", .

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

.

..

, ...

+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))
    }
+2

, . mapView.

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

- GMSPath , mapView - GmsMapView.

0

Source: https://habr.com/ru/post/1626597/


All Articles