ios - 如何删除 map 路线的这条折线?

标签 ios swift

大家好,我想做的是创建一个触发 findRandomPlace() 函数的按钮。它应该向我展示 places 数组中的一个随机位置。当它给我一个随机位置时,它应该绘制从源到目的地的折线(它确实如此),然后如果我不喜欢那个地方,我可以再次单击按钮,它应该会显示另一个地方(它确实如此) ,问题是最后一个地方的折线。我不知道如何删除它。

let route = response.routes[0]
self.Map.add(route.polyline, level: .aboveRoads)

上面的代码是绘制折线的代码,但我不确定如何删除它。 如果您能提出任何想法,请提前致谢。

var previousIndex:Int = 999999
func findRandomPlace() {//need an if statement to check if its near u or in the area
    let randomIndex = Int(arc4random_uniform(UInt32(places.count)))

    if randomIndex != previousIndex {//Makes sure next place can't be current place
        previousIndex = randomIndex

        Name.text = (places[randomIndex][0])
        Description.text = (places[randomIndex][1])


        //Draw Direction Line
        let positionX:Double? = Double(places[randomIndex][2])
        let positionY:Double? = Double(places[randomIndex][3])

        let sourceCoordinates = manager.location?.coordinate
        let destinationCoordinates = CLLocationCoordinate2DMake(positionX!, positionY!)
        print(sourceCoordinates!)

        let sourcePlacemark = MKPlacemark(coordinate: sourceCoordinates!)
        let destinationPlacemark = MKPlacemark(coordinate: destinationCoordinates)

        let sourceItem = MKMapItem(placemark: sourcePlacemark)
        let destinationItem = MKMapItem(placemark: destinationPlacemark)

        let directionRequest = MKDirectionsRequest()
        directionRequest.source = sourceItem
        directionRequest.destination = destinationItem
        directionRequest.transportType = .walking

        let directions = MKDirections(request: directionRequest)
        directions.calculate(completionHandler: {
            response, error in
            //Draw Polyline and Zoom Into It
            guard let response = response else {
                if let error = error {
                    print("error")
                }
                return
            }
            let route = response.routes[0]
            self.Map.add(route.polyline, level: .aboveRoads)
            let rect = route.polyline.boundingMapRect
            self.Map.setRegion(MKCoordinateRegionForMapRect(rect), animated: true)
        })
        } else {

            findRandomPlace()
        }

}

最佳答案

我自己想通了。 我很惭愧花了这么长时间,但我很高兴我最终找到了答案。

self.Map.removeOverlays(self.Map.overlays)

关于ios - 如何删除 map 路线的这条折线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50745152/

相关文章:

ios - 连续动画 - Watchkit

arrays - 如何创建一个函数来将字符串与字典中的字符串数组进行比较并根据键分配点?

objective-c - "[AnyObject]"与 "NSArray"不同

ios - 仅使用 UIImageView 调整大小、居中和图像

带有 Qt 的 iOS 应用程序?

ios - 用于“打开方式”菜单的 UIDocumentInteractionController - 不起作用

ios - DispatchTime.asyncafter 的一致性

ios - 使用 constraintsWithVisualFormat 的自动布局效果不佳

ios - 更改照片编辑扩展的标题

ios - UITableViewCell 缩进仅适用于默认单元格 textLabel