From 4fc1de3a95f846c13963f316166114d39dd5dcad Mon Sep 17 00:00:00 2001 From: Joseph Van Boxtel Date: Fri, 14 May 2021 09:21:15 -0700 Subject: [PATCH] Implement getVisibleRegion() with MKMapView.region This implementation respects the `padding` value and insets the region based on that padding. This allows setting the bounds and reading the bounds to have the same semantics with respect to the `padding`. --- ios/Classes/MapView/MapViewExtension.swift | 28 ++++++---------------- 1 file changed, 7 insertions(+), 21 deletions(-) diff --git a/ios/Classes/MapView/MapViewExtension.swift b/ios/Classes/MapView/MapViewExtension.swift index 8438a08..11d32d5 100644 --- a/ios/Classes/MapView/MapViewExtension.swift +++ b/ios/Classes/MapView/MapViewExtension.swift @@ -228,32 +228,18 @@ public extension MKMapView { func getVisibleRegion() -> Dictionary> { if self.bounds.size != CGSize.zero { - // convert center coordiate to pixel space - let centerPixelX = self.longitudeToPixelSpaceX(longitude: self.centerCoordinate.longitude) - let centerPixelY = self.latitudeToPixelSpaceY(latitude: self.centerCoordinate.latitude) - - // determine the scale value from the zoom level - let zoomExponent = Double(21 - Holder._zoomLevel) - let zoomScale = pow(2.0, zoomExponent) - - // scale the map’s size in pixel space - let mapSizeInPixels = self.bounds.size - let scaledMapWidth = Double(mapSizeInPixels.width) * zoomScale - let scaledMapHeight = Double(mapSizeInPixels.height) * zoomScale; - - // figure out the position of the top-left pixel - let topLeftPixelX = centerPixelX - (scaledMapWidth / 2); - let topLeftPixelY = centerPixelY - (scaledMapHeight / 2); + let center = self.region.center + let span = self.region.span // find the southwest coordinate - let minLng = self.pixelSpaceXToLongitude(pixelX: topLeftPixelX) - let minLat = self.pixelSpaceYToLatitude(pixelY: topLeftPixelY) + let minLng = center.longitude - span.longitudeDelta/2 + let minLat = center.latitude - span.latitudeDelta/2 // find the northeast coordinate - let maxLng = self.pixelSpaceXToLongitude(pixelX: topLeftPixelX + scaledMapWidth) - let maxLat = self.pixelSpaceYToLatitude(pixelY: topLeftPixelY + scaledMapHeight) + let maxLng = center.longitude + span.longitudeDelta/2 + let maxLat = center.latitude + span.latitudeDelta/2 - return ["northeast": [minLat, maxLng], "southwest": [maxLat, minLng]] + return ["northeast": [maxLat, maxLng], "southwest": [minLat, minLng]] } return ["northeast": [0.0, 0.0], "southwest": [0.0, 0.0]] }