Как добавить аннотацию в конце моего маршрута? - PullRequest
0 голосов
/ 06 сентября 2018

Я рассчитываю маршрут и хочу добавить аннотацию булавки в конце ...

объявляет местоположение, отправляет запрос, объявляет маршруты и сбрасывает вид моей карты с новыми указаниями ...

guard let location = locationManager.location?.coordinate else { return }
    let request = createDirectionsRequest(from: location)
    let directions = MKDirections(request: request)
    resetMapView(withNew: directions)

после того, как я вычислю направление и добавлю аннотацию ...

directions.calculate { [unowned self] (response, error) in
        guard let response = response else { return }

        for route in response.routes {
            self.mapView.add(route.polyline, level: MKOverlayLevel.aboveRoads)
            self.mapView.setCenter(route.polyline.coordinate, animated: true)
            self.mapView.setVisibleMapRect(route.polyline.boundingMapRect, animated: true)
            self.mapView.setRegion(MKCoordinateRegionMakeWithDistance(route.polyline.coordinate, route.distance*0.75, route.distance*0.75), animated: true)

            let routeAnnotation = MKPointAnnotation()
            routeAnnotation.coordinate = MKCoordinateForMapPoint(route.polyline.points()[route.polyline.pointCount/2])
            self.mapView.addAnnotation(routeAnnotation)
        }
    }

и это результат ... аннотация находится не в правильном положении моего маршрута ... где я ошибаюсь?

enter image description here

UPDATE

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

func getCenterLocation(for mapView: MKMapView) -> CLLocation {
    let latidude = mapView.centerCoordinate.latitude
    let longitude = mapView.centerCoordinate.longitude

    return CLLocation(latitude: latidude, longitude: longitude)
}

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

routeAnnotation.coordinate = self.getCenterLocation(for: self.mapView).coordinate

и это результат

enter image description here

принимаются другие решения ...

...