简体   繁体   English

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

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

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. 折线是通过google地图方向api中的overview_polyline获得的。

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

}

For Swift 2.0 Google Maps, to make your map view fit the polyline of the route you are drawing: 对于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))

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. GMSPolyLine具有属性“.path”,可用于将地图与完整折线本身绑定。

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

With this approach, It'll do the job for sure. 有了这种方法,它肯定会完成这项工作。

Coz it has Done for me.. 因为它已经完成了我..

Hope it Helps.. 希望能帮助到你..

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

You can zoom the map according the route or line displayed in the mapview. 您可以根据mapview中显示的路线或线条缩放地图。 So that your route is displayed with in the mapView bounds. 这样您的路线就会在mapView界限中显示出来。

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. 这里路径是路径的GMSPath,mapView是GmsMapView。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM