My question is about a map service I'm using a map with custom tile service now I need to add two buttons or a stepper to zoom in and zoom out and I'm using MapKit library. can someone help with that?
Asked
Active
Viewed 3,994 times
-1
-
1I have answer but in objective c – Jitendra Modi Aug 28 '17 at 10:09
2 Answers
5
Check out answer
switch sender.tag {
case 10:
//Zoom In
var region: MKCoordinateRegion = map_view.region
region.span.latitudeDelta /= 2.0
region.span.longitudeDelta /= 2.0
map_view.setRegion(region, animated: true)
case 20:
//Zoom Out
var region: MKCoordinateRegion = map_view.region
region.span.latitudeDelta = min(region.span.latitudeDelta * 2.0, 180.0)
region.span.longitudeDelta = min(region.span.longitudeDelta * 2.0, 180.0)
map_view.setRegion(region, animated: true)
}

Jitendra Modi
- 2,344
- 12
- 34
3
Here is the answer in swift 3
for zoom In
let region = MKCoordinateRegionMake(self.mapView.region.center, MKCoordinateSpanMake(mapView.region.span.latitudeDelta*0.7, mapView.region.span.longitudeDelta*0.7))
mapView.setRegion(region, animated: true)
for zoom out
let zoom = getZoom() // to get the value of zoom of your map.
if zoom > 3.5{ // **here i have used the condition that avoid the mapview to zoom less then 3.5 to avoid crash.**
let region = MKCoordinateRegionMake(self.mapView.region.center, MKCoordinateSpanMake(mapView.region.span.latitudeDelta/0.7, mapView.region.span.longitudeDelta/0.7))
mapView.setRegion(region, animated: true)
}
Here you can get the how much the map view is zoomed.
func getZoom() -> Double {
var angleCamera = self.mapView.camera.heading
if angleCamera > 270 {
angleCamera = 360 - angleCamera
} else if angleCamera > 90 {
angleCamera = fabs(angleCamera - 180)
}
let angleRad = Double.pi * angleCamera / 180
let width = Double(self.view.frame.size.width)
let height = Double(self.view.frame.size.height)
let heightOffset : Double = 20
let spanStraight = width * self.mapView.region.span.longitudeDelta / (width * cos(angleRad) + (height - heightOffset) * sin(angleRad))
return log2(360 * ((width / 256) / spanStraight)) + 1;
}
It works for me.

Amrit Tiwari
- 922
- 7
- 21