【问题标题】:How to draw a route between two markers in google maps (swift )?如何在谷歌地图(swift)中的两个标记之间绘制路线?
【发布时间】:2016-10-16 08:27:45
【问题描述】:

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

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

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)

    }

这里是点击删除标记的代码:

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
    }

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

【问题讨论】:

    标签: swift google-maps routes direction


    【解决方案1】:

    使用Polylines 可以在地图上画线。您需要通过创建具有两个或更多点的相应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
    

    检查这些相关链接:

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

    希望这会有所帮助! :)

    【讨论】:

    【解决方案2】:

    使用GMSPolyline 并将其map 属性设置为您的GMSMapView 以在您的地图上绘制折线/路线

    【讨论】:

    • 怎么用?!
    • 和上面一样使用GMSMarker
    猜你喜欢
    • 1970-01-01
    • 2017-06-27
    • 1970-01-01
    • 2018-01-02
    • 1970-01-01
    • 2021-05-15
    • 2016-02-27
    • 2016-02-12
    相关资源
    最近更新 更多