gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 01:57:30 25 4
gpt4 key购买 nike

大家好,我想做的是创建一个触发 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com