首页 文章

Google Maps for iOS,swift - 如何在标记之间显示整个折线?

提问于
浏览
4

我想在谷歌 Map 视图中拟合折线 . 折线是通过google Map 方向api中的overview_polyline获得的 .

想知道如何将编码的折线转换成可以使用的东西 . 我需要在 Map 视图中拟合折线 . 我发现要做的就是适合显示所有标记但不显示整个折线的边界 .

func fitAllMarkers()
{

    var bounds = GMSCoordinateBounds()

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

    googleMapView.animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds))

}

3 回答

  • 2

    对于Swift 2.0 Google Map ,要使 Map 视图适合您绘制的路线的折线:

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

    虽然这个问题有一个公认的答案,

    还有一点尚未讨论过 .

    GMSPolyLine具有属性“.path”,可用于将 Map 与完整折线本身绑定 .

    mapView.animate(with: GMSCameraUpdate.fit(GMSCoordinateBounds(path: self.polyLineObject.path!), withPadding: 10))
    
    • mapView是GMSMapView()的一个实例

    • polyLineObject是GMSPolyLine()的一个实例

    有了这种方法,它肯定会完成这项工作 .

    因为它已经完成了我..

    希望能帮助到你..

  • 3
    **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))
        }
    

相关问题