문제 설명
Is there a way to restrict MKMapView Zoomlevel in iOS6 (Is there a way to restrict MKMapView Zoomlevel in iOS6)
In one of my iPad apps, I use MKMapView to show regions and I have a weird issue. I use setRegion: animated: method to set a particular region from a noOf points. When I zoom from that particular point in the map, the map shows grids with no image of the place/location. Does anyone has the same issue before? Can someone provide a solution for how to handle this zoom level issue?
Attaching the image for more reference
Initially, I call the setRegion: method of mapview with a 2d co‑ordinate and its span as 0.0014f for both latitude and longitude delta values. So I get the following image.
After that if I try to zoom in the mapview again, it goes to a situation like the following
My concern is, whether there is a way to restrict the zoom in to avoid the black layer display/how can I restrict the zoom in once mapview reaches its best possible zoomed cells.
참조 솔루션
방법 1:
when you zoom in CLOSE the mapview wants to show the most detailed tiles and nothin else. For certain areas (gov, military or places where there is nothing) there are no tiles and it runs into that!
the problem is that you dont know beforehand where there are or aren't any tiles available.
what I could imagine: when you zoom. regionWillChange:
is called and then willStartLoading:
should be called and then mapViewDidFailLoadingMap:withError
in the case of no tiles
(im guessing here)
방법 2:
In Swift just you should use from following code for define minimum and maximum zoom limitation
let MIN_ZOOM_LEVEL = 16.5
let MAX_ZOOM_LEVEL = 18.0
extension UIViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
let coordinate = CLLocationCoordinate2DMake(mapView.region.center.latitude, mapView.region.center.longitude)
let zoomLevel = getZoomLevel()
if MIN_ZOOM_LEVEL > zoomLevel || MAX_ZOOM_LEVEL < zoomLevel {
let region = MKCoordinateRegionMake(coordinate, self.latestSpan)
mapView.setRegion(region, animated:true)
} else {
self.latestSpan = MKCoordinateSpanMake(0, 360 / pow(2, Double(zoomLevel‑1)) * Double(mapView.frame.size.width) / 256)
}
}
}
func getZoomLevel() ‑> Double {
var angleCamera = mapView.camera.heading
if angleCamera > 270 {
angleCamera = 360 ‑ angleCamera
} else if angleCamera > 90 {
angleCamera = fabs(angleCamera ‑ 180)
}
let angleRad = M_PI * angleCamera / 180
let width = Double(mapView.frame.size.width)
let height = Double(mapView.frame.size.height)
let heightOffset : Double = 20
let spanStraight = width * mapView.region.span.longitudeDelta / (width * cos(angleRad) + (height ‑ heightOffset) * sin(angleRad))
return log2(360 * ((width / 256) / spanStraight)) + 1
}
(by Sagar S. Kadookkunnan、Daij‑Djan、Saeed)