MKMapView Bounds - PullRequest
       35

MKMapView Bounds

0 голосов
/ 11 июня 2019

Я иду из Google Maps в Apple Maps.Карты Google имеют возможность обновлять камеру на основе координат северо-востока и юго-запада следующим образом:

let bounds = GMSCameraUpdate.fit(GMSCoordinateBounds(northEastSouthWestBounds), with: .zero)
self.mapView.moveCamera(bounds)

Я знаю, что могу использовать setVisibleMapRect(_:animated:), и это занимает MKMapRect.Мой реальный вопрос - как я могу создать MKMapRect на основе координат северо-востока (CLLocation) и координат юго-запада (CLLocation).

1 Ответ

0 голосов
/ 11 июня 2019

Создайте MKMapRect из ваших координат и передайте его либо setVisibleMapRect(_:animated:) или setVisibleMapRect(_:edgePadding:animated:).

Для созданияMKMapRect из массива различных типов точек:

import MapKit

extension Array where Element == CLLocationCoordinate2D {
    func mapRect() -> MKMapRect? {
        return map(MKMapPoint.init).mapRect()
    }
}

extension Array where Element == CLLocation {
    func mapRect() -> MKMapRect? {
        return map { MKMapPoint($0.coordinate) }.mapRect()
    }
}

extension Array where Element == MKMapPoint {
    func mapRect() -> MKMapRect? {
        guard count > 0 else { return nil }

        let xs = map { $0.x }
        let ys = map { $0.y }

        let west = xs.min()!
        let east = xs.max()!
        let width = east - west

        let south = ys.min()!
        let north = ys.max()!
        let height = north - south

        return MKMapRect(x: west, y: south, width: width, height: height)
    }
}
...