Как рассчитать высоту, необходимую для отображения полного MKCoordinateRegion в MKMapView? - PullRequest
1 голос
/ 26 января 2020

Итак, я хочу установить определенную c позицию с указанным c регионом, показанным при заданном c вращении.

Я знаю, что есть setRegion, который вычисляет высоту автоматически, но это не позволяет вам установить вращение. Чтобы установить вращение, мне нужно использовать setRotation, который принимает центр, высоту и курс.

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

Я пытаюсь:

func updateUIView(_ view: ZooMap, context: Context) {
    let coordinate = CLLocationCoordinate2D(
        latitude: 21.270381425220606,
        longitude: -157.81908260613335
    )
    let span = MKCoordinateSpan(latitudeDelta: 0.007075784913642025, longitudeDelta: 0.010196763428524491)
    let region = MKCoordinateRegion(center: coordinate, span: span)

    let camera = MKMapCamera()
    camera.heading = 71
    camera.centerCoordinate = coordinate;
    camera.altitude = determineAltitudeForRegion(region: region, heading: camera.heading, viewport: UIScreen.main.bounds.size)

    view.setCamera(camera, animated: false)
}

func determineAltitudeForRegion(region: MKCoordinateRegion, heading: Double, viewport: CGSize) -> Double {
    // Calculate a new bounding rectangle that is corrected for the aspect ratio
    // of the viewport/camera -- this will be needed to ensure the resulting
    // altitude actually fits the polygon in view for the observer.
    let upperLeftCoord = CLLocationCoordinate2DMake(region.center.latitude + region.span.latitudeDelta / 2, region.center.longitude - region.span.longitudeDelta / 2)
    let upperRightCoord = CLLocationCoordinate2DMake(region.center.latitude + region.span.latitudeDelta / 2, region.center.longitude + region.span.longitudeDelta / 2)
    let lowerLeftCoord = CLLocationCoordinate2DMake(region.center.latitude - region.span.latitudeDelta / 2, region.center.longitude - region.span.longitudeDelta / 2)

    let hDist = MKMapPoint(upperLeftCoord).distance(to: MKMapPoint(upperRightCoord));
    let vDist = MKMapPoint(upperLeftCoord).distance(to: MKMapPoint(lowerLeftCoord));

    var adjacent: Double = 0;
    var newHDist: Double = 0;
    var newVDist: Double = 0;
    let rect = RectForRegion(region: region)

    if (rect.size.height > rect.size.width) {
        newVDist = (Double(viewport.height) / Double(viewport.width)) * hDist;
        adjacent = newVDist;
    } else {
        newHDist = (Double(viewport.width) / Double(viewport.height)) * vDist;
        adjacent = newHDist;
    }

    return adjacent / tan(heading.degreesToRadians) / 2
}

func RectForRegion(region: MKCoordinateRegion) -> MKMapRect {
    let a: MKMapPoint = MKMapPoint(CLLocationCoordinate2DMake(region.center.latitude + region.span.latitudeDelta / 2, region.center.longitude - region.span.longitudeDelta / 2))
    let b: MKMapPoint = MKMapPoint(CLLocationCoordinate2DMake(region.center.latitude - region.span.latitudeDelta / 2, region.center.longitude + region.span.longitudeDelta / 2))
    return MKMapRect(x: min(a.x, b.x), y: min(a.y, b.y), width: abs(a.x - b.x), height: abs(a.y - b.y))
}

, но он не работает должным образом. Как мне это сделать ??

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...