How can I calculate the max zoom out in MapKit to show the entire globe?
Example:
let region: MKCoordinateRegion = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: -37.8136, longitude: 144.9631), span: MKCoordinateSpan(latitudeDelta: 170, longitudeDelta: 170))
If instead of 170 I put 200 the App crash telling me:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid Region <center:-37.81360000, +144.96310000 span:+200.00000000, +200.00000000>'
2
Answers
The Earth has 2 hemispheres, divided into 90 degrees each. So latitude range is from -90 to 90 i.e.
latitudeDelta
cannot be more than 180 degrees.Longitude range is from -180 to 180, so
longitudeDelta
max is 360 degrees.To show a globe, set
MapView.mapType = .hybridFlyover
orMapView.mapType = .satelliteFlyover
.When initializing
MKCoordinateRegion
with coordinate and span and useMKMapView.setRegion(_ region: MKCoordinateRegion, animated: Bool)
Ref: MKMapView.setRegion(_ region: MKCoordinateRegion, animated: Bool)
Here is my workaround with
MKMapCamera
.Code in MapScreen.swift
MapView layout in Xib (I’m using fullscreen layout)
This is result on Portrait mode,
focusOnPointAndShowGlobe()
will focus on center location then move away from the Earth to disply entire globe, when you open this screen or callfocusOnPointAndShowGlobe()
again.