MapKit - 找到路线但未显示
MapKit - Route is found but not displaying
我正在 iPhone 模拟器中创建一条从用户当前位置到另一点的路线。路线创建得很好,我能够掌握到达目的地所需的时间和距离,但路线 polyline/overlay 未显示。代码相当大,所以下面是我认为解释问题所需的基本部分。
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 的委托,以便它知道在哪里寻找渲染器?
我正在 iPhone 模拟器中创建一条从用户当前位置到另一点的路线。路线创建得很好,我能够掌握到达目的地所需的时间和距离,但路线 polyline/overlay 未显示。代码相当大,所以下面是我认为解释问题所需的基本部分。
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 的委托,以便它知道在哪里寻找渲染器?