gpt4 book ai didi

swift - MapKit - 找到路线但未显示

转载 作者:行者123 更新时间:2023-11-30 10:37:59 25 4
gpt4 key购买 nike

我正在 iPhone 模拟器中创建一条从用户当前位置到另一个点的路线。路线创建得很好,我能够获取到达目的地所需的时间和距离,但路线折线/叠加层未显示。代码相当大,因此以下是我认为解释问题所需的基本部分。

func tapTocreateRoute(){
location = CLLocationCoordinate2D(latitude: 37.787359, longitude: -122.41)
let placemark = MKPlacemark(coordinate: location)
let mapItem = MKMapItem(placemark: placemark)
routeCreater(mapItem: mapItem)
}

func routeCreater(mapItem: MKMapItem){

let destinationPin = customPin(pinTitle: "end", pinSubtitle: "testing", location: carLocation)
self.mapView.addAnnotation(destinationPin)

let request = MKDirections.Request()
request.source = MKMapItem.forCurrentLocation()
request.destination = mapItem
request.transportType = MKDirectionsTransportType.walking

let directions = MKDirections(request: request)

directions.calculate { (response, error) in
guard let response = response else {
print("MKDIRECTION error: \(error?.localizedDescription ?? "Error not found")")
return
}

self.route = response.routes[0]
self.mapView.addOverlay(self.route.polyline, level: .aboveRoads)

self.mapView.setVisibleMapRect(self.route.polyline.boundingMapRect, animated: true)
let rect = self.route.polyline.boundingMapRect
self.mapView.setRegion(MKCoordinateRegion(rect), animated: true)

self.walkingTime = self.route.expectedTravelTime //displays correctly
self.walkingDistance = self.route.distance // displays correctly

}
}

//Line Renderer for Route
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let lineRenderer = MKPolylineRenderer(overlay: overlay)
lineRenderer.strokeColor = UIColor(red: 216/255, green: 71/255, blue: 30/255, alpha: 1)
lineRenderer.lineWidth = 3.0
return lineRenderer
}

渲染时我遗漏了什么吗?感谢您的帮助

最佳答案

您是否确保分配了 MapView 的委托(delegate),以便它知道在哪里寻找渲染器?

关于swift - MapKit - 找到路线但未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57505853/

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