如何在谷歌地图(swift)中的两个标记之间绘制路线?

P.T*_*.Tb 3 google-maps routes direction swift

我想显示我制作的两个标记之间的方向。我该怎么做?如何显示方向?

这是我用来制作标记的代码:

func mapView(mapView: GMSMapView, didLongPressAtCoordinate coordinate: CLLocationCoordinate2D) {
    if counterMarker < 2
    {
        counterMarker += 1
        let marker = GMSMarker(position: coordinate)
        marker.appearAnimation = kGMSMarkerAnimationPop

        marker.map = mapView
        marker.position.latitude = coordinate.latitude
        marker.position.longitude = coordinate.longitude

        print(marker.position.latitude)
        print(marker.position.longitude)

    }
Run Code Online (Sandbox Code Playgroud)

这是用于在单击时删除标记的代码:

func mapView(mapView: GMSMapView, didTapMarker marker: GMSMarker) -> Bool {

        let alert = UIAlertController(title: "Alert", message: "Are you Sure for deleting ?!", preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "No", style: UIAlertActionStyle.Default) {
            UIAlertAction in
            NSLog("No Pressed")


            })
        alert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default) {
            UIAlertAction in
            NSLog("Yes Pressed")
            marker.map = nil
            self.counterMarker -= 1

        })

        self.presentViewController(alert, animated: true, completion: nil)

        return true
    }
Run Code Online (Sandbox Code Playgroud)

我喜欢显示哪个标记是目的地,哪个是起点。

abi*_*ita 6

使用允许您在地图上绘制线条的折线。您需要通过创建GMSMutablePath具有两个或更多点的相应对象来指定其路径。每个CLLocationCoordinate2D代表地球表面上的一个点。线段根据您将它们添加到路径中的顺序在点之间绘制。

例子:

let path = GMSMutablePath()
path.addCoordinate(CLLocationCoordinate2D(latitude: 37.36, longitude: -122.0))
path.addCoordinate(CLLocationCoordinate2D(latitude: 37.45, longitude: -122.0))
path.addCoordinate(CLLocationCoordinate2D(latitude: 37.45, longitude: -122.2))
path.addCoordinate(CLLocationCoordinate2D(latitude: 37.36, longitude: -122.2))
path.addCoordinate(CLLocationCoordinate2D(latitude: 37.36, longitude: -122.0))

let rectangle = GMSPolyline(path: path)
rectangle.map = mapView
Run Code Online (Sandbox Code Playgroud)

检查这些相关链接:

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

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))
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!:)