Swift - mapView с маршрутом в пользовательской ячейке таблицы - PullRequest
0 голосов
/ 11 апреля 2019

Я пытаюсь реализовать mapView в пользовательской ячейке таблицы, которая отображает маршрут между двумя точками, но я изо всех сил пытаюсь получить маршрут для отображения.

Вот мой код:

override func tableView(_ tableView: UITableView,
                        cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    // Get a cell to display the workout in
    let cell:ChallengeTableCell = self.tableView.dequeueReusableCell(withIdentifier: "challengeTableCell") as! ChallengeTableCell

    cell.mapView.delegate = self

            let initialLocation = CLLocation(latitude: 51.5074, longitude: -0.1278)
            let regionRadius: CLLocationDistance = 25000
            let coordinateRegion = MKCoordinateRegion(center: initialLocation.coordinate,
                                                      latitudinalMeters: regionRadius, longitudinalMeters: regionRadius)
            cell.mapView.setRegion(coordinateRegion, animated: false)

            let startPoint = CLLocationCoordinate2D(latitude: 51.503794, longitude: -0.502074)
            let endPoint = CLLocationCoordinate2D(latitude: 51.498665, longitude: 0.269378)
            let sourcePlacemark = MKPlacemark(coordinate: startPoint, addressDictionary: nil)
            let destinationPlacemark = MKPlacemark(coordinate: endPoint, addressDictionary: nil)

            let sourceMapItem = MKMapItem(placemark: sourcePlacemark)
            let destinationMapItem = MKMapItem(placemark: destinationPlacemark)

            let directionRequest = MKDirections.Request()
            directionRequest.source = sourceMapItem
            directionRequest.destination = destinationMapItem
            directionRequest.transportType = .automobile

            let directions = MKDirections(request: directionRequest)

            directions.calculate {
                (response, error) -> Void in

                guard let response = response else {
                    if let error = error {
                        print("Error: \(error)")
                    }

                    return
                }

                let route = response.routes[0]
                print(route)
                cell.mapView.addOverlay((route.polyline), level: MKOverlayLevel.aboveRoads)

                let rect = route.polyline.boundingMapRect
                cell.mapView.setRegion(MKCoordinateRegion(rect), animated: false)
            }

Который должен вызываться методом делегата:

    func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer {
    let renderer = MKPolylineRenderer(overlay: overlay)
    renderer.strokeColor = UIColor.red
    renderer.lineWidth = 4.0

    return renderer
}

Однако, когда я тестирую код, маршрут не рисуется, и я не уверен, что делать дальше.

...