本文介绍了我如何删除Swift 3中的GMSPolyline的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在GoogleMap中制作GMSPolyline。当我想通过编码 polyline.map = nil 来删除GMSPolyline时,但它无法为我工作。



(之前添加了标记,我只需要移除折线)

感谢您的帮助!!!

我的编码:

  func mapView(_ mapView:GMSMapView,didTap标记:GMSMarker) - > Bool {
// MARK:创建GMSPolyline
让polyline = GMSPolyline()
// MARK:从GoogleMap中移除旧的折线
polyline.map = nil

let origin =\(mapView.myLocation!.coordinate.latitude),\(mapView.myLocation!.coordinate.longitude)
let destination =\(marker.position。纬度),\(marker.position.longitude)

let url =https://maps.googleapis.com/maps/api/directions/json?origin=\(origin) & destination = \(destination)& mode = driving

Alamofire.request(url).responseJSON {response in

let json = JSON(data:response .data!)
let routes = json [routes]。arrayValue

// MARK:使用Polyline打印路由
用于路由的路由
{
let routeOverviewPolyline = route [overview_polyline]。dictionary
let points = routeOverviewPolyline?[points]?。stringValue
让path = GMSPath(fromEncodedPath:points!)
polyline = GMSPolyline(path:path)
polyline.strokeWidth = 4
polyline.strokeColor = UIColor.yellow
polyline.isTappable = true
polyline.map = self.mapView
}
}
return false
}


解决方案

在地图上添加新的折线之前,您应该先清除地图。 GMSMapView的clear()函数

  / ** 

清除已添加到地图,包括标记,多义线和地面叠加层。
这不会清除可见的位置点或重置当前的mapType。

* /

试试这个

  func mapView(_ mapView:GMSMapView,didTap marker:GMSMarker) - > Bool {
// MARK:创建GMSPolyline
让polyline = GMSPolyline()
// MARK:从GoogleMap中删除旧的多段线
mapView.clear()
// TODO: - 在这里重绘所有标记。
let origin =\(mapView.myLocation!.coordinate.latitude),\(mapView.myLocation!.coordinate.longitude)
let destination =\(marker.position.latitude ),\(marker.position.longitude)

let url =https://maps.googleapis.com/maps/api/directions/json?origin=\(origin )& ; destination = \(destination)& mode = driving

Alamofire.request(url).responseJSON {response in

let json = JSON(data:response。数据!)
let routes = json [routes]。arrayValue

// MARK:使用Polyline打印路线
用于路线的路线
{
让routeOverviewPolyline = route [overview_polyline]。字典
let points = routeOverviewPolyline?[points] ?. stringValue
let path = GMSPath(fromEncodedPath:points!)
polyline = GMSPolyline (路径:路径)
polyline.strokeWidth = 4
polyline.strokeColor = UIColor.yellow
polyline.isTappable = true
polyline.map = self.mapView
}
}
返回false
}


I'm making the GMSPolyline in the GoogleMap. When I want to remove the GMSPolyline by coding polyline.map = nil, but it can not working for me. I'm copy some coding in the below.

(added the marker before, I need remove the polyline only)

Thank you for your help!!!

my coding:

func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
    //MARK: create the GMSPolyline
    let polyline = GMSPolyline()
    //MARK: remove the old polyline from the GoogleMap
    polyline.map = nil

    let origin = "\(mapView.myLocation!.coordinate.latitude),\(mapView.myLocation!.coordinate.longitude)"
    let destination = "\(marker.position.latitude),\(marker.position.longitude)"

    let url = "https://maps.googleapis.com/maps/api/directions/json?origin=\(origin)&destination=\(destination)&mode=driving"

    Alamofire.request(url).responseJSON { response in

        let json = JSON(data: response.data!)
        let routes = json["routes"].arrayValue

        //MARK: print route using Polyline
        for route in routes
        {
            let routeOverviewPolyline = route["overview_polyline"].dictionary
            let points = routeOverviewPolyline?["points"]?.stringValue
            let path = GMSPath(fromEncodedPath: points!)
            polyline = GMSPolyline(path: path)
            polyline.strokeWidth = 4
            polyline.strokeColor = UIColor.yellow
            polyline.isTappable = true
            polyline.map = self.mapView
        }
    }
    return false
}
解决方案

you should clear the map before adding new polyline on the map.

clear() function of GMSMapView

/**

Clears all markup that has been added to the map, including markers, polylines and ground overlays.
This will not clear the visible location dot or reset the current mapType.

*/

Try this

func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
    //MARK: create the GMSPolyline
    let polyline = GMSPolyline()
    //MARK: remove the old polyline from the GoogleMap
    mapView.clear()
    //TODO:- Redraw all marker here.
    let origin = "\(mapView.myLocation!.coordinate.latitude),\(mapView.myLocation!.coordinate.longitude)"
    let destination = "\(marker.position.latitude),\(marker.position.longitude)"

    let url = "https://maps.googleapis.com/maps/api/directions/json?origin=\(origin)&destination=\(destination)&mode=driving"

    Alamofire.request(url).responseJSON { response in

        let json = JSON(data: response.data!)
        let routes = json["routes"].arrayValue

        //MARK: print route using Polyline
        for route in routes
        {
            let routeOverviewPolyline = route["overview_polyline"].dictionary
            let points = routeOverviewPolyline?["points"]?.stringValue
            let path = GMSPath(fromEncodedPath: points!)
            polyline = GMSPolyline(path: path)
            polyline.strokeWidth = 4
            polyline.strokeColor = UIColor.yellow
            polyline.isTappable = true
            polyline.map = self.mapView
        }
    }
    return false
}

这篇关于我如何删除Swift 3中的GMSPolyline的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 06:54