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

时间:2016-01-29 22:24:59

标签: ios swift google-maps google-polyline

我正在尝试在谷歌地图视图中插入折线。折线是通过google地图方向api中的overview_polyline获得的。

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

func fitAllMarkers()
{

    var bounds = GMSCoordinateBounds()

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

    googleMapView.animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds))

}

4 个答案:

答案 0 :(得分:9)

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

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

答案 1 :(得分:5)

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

还有一点尚未讨论过。

GMSPolyLine有一个属性“.path”,可用于将地图与完整的折线本身绑定。

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

通过这种方法,它肯定会完成这项工作。

因为它已经为我完成了..

希望它有助于......

答案 2 :(得分:2)

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

答案 3 :(得分:0)

您可以根据地图视图中显示的路线或线来缩放地图。这样您的路线就会显示在mapView边界内。

fatal: renaming '/static/CSS' failed: Invalid argument

此处路径为路线的GMSPath,而mapView为GmsMapView。