Skip to content

Commit 670c9c3

Browse files
committed
implemented restrecting the zoom level by supporting zoom level of satellite imagery.
1 parent e09ad46 commit 670c9c3

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

GoInfoGame/GoInfoGame/Helpers/Extensions.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,21 @@ extension MKMapView {
6262
func isZoomedIn(maxLatitudeDelta: CLLocationDegrees = 0.005) -> Bool {
6363
return self.region.span.latitudeDelta <= maxLatitudeDelta
6464
}
65+
66+
func distanceForZoom(zoomLevel: Int) -> CLLocationDistance {
67+
// Earth's circumference in meters
68+
let earthCircumference: Double = 40075016.686
69+
// Standard tile size (pixels)
70+
let tileSize: Double = 256
71+
// Get the width of the map in points and scale by screen
72+
let scale = UIScreen.main.scale
73+
let mapWidthInPixels = Double(self.frame.size.width) * scale
74+
// Calculate meters per pixel at equator for the given zoom
75+
let metersPerPixel = earthCircumference / (tileSize * pow(2.0, Double(zoomLevel)))
76+
// The distance (in meters) visible in the current map width
77+
return metersPerPixel * mapWidthInPixels
78+
}
79+
6580
}
6681

6782
extension CLLocationCoordinate2D: CustomPersistable {

GoInfoGame/GoInfoGame/UI/Map/MapView.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,11 @@ struct MapView: View {
265265
selected: $viewModel.selectedOption,
266266
onSelect: { selected in
267267
viewModel.selectedOption = selected
268+
if case .wmts(let wmts) = selected {
269+
mapViewRef?.setCameraZoomRange(MKMapView.CameraZoomRange(minCenterCoordinateDistance: mapViewRef?.distanceForZoom(zoomLevel: wmts.extent.maxZoom) ?? 0.0), animated: true)
270+
} else {
271+
mapViewRef?.setCameraZoomRange(nil, animated: true)
272+
}
268273
viewModel.showSatellitePicker = false
269274
addOverlay(selectedSatilliteOption: selected)
270275
}

0 commit comments

Comments
 (0)