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

后端 未结 4 660
Happy的楠姐
Happy的楠姐 2021-01-12 13:19

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 b

相关标签:
4条回答
  • 2021-01-12 13:46

    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.

    0 讨论(0)
  • 2021-01-12 13:52
    **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))
        }
    
    0 讨论(0)
  • 2021-01-12 13:58

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

    0 讨论(0)
  • 2021-01-12 14:12

    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))
    
    0 讨论(0)
提交回复
热议问题