Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there any way of allowing zooming MKMapView only to a specific region, while disabling any other user interaction?

I have a swift app with a MapView. These are my settings on story board for the map:

enter image description here

Also, in the code I'm doing:

let regionRadius: CLLocationDistance = 10000
let initialLocation = CLLocation(latitude: latitude, longitude: longitude)
centerMapOnLocation(initialLocation, map: cell.mapView, radius: regionRadius)
cell.mapView.scrollEnabled = false
cell.mapView.rotateEnabled = false

My method centerMapOnLocation centers the map initially on a specific location:

func centerMapOnLocation(location: CLLocation, map: MKMapView, radius: CLLocationDistance) {
    let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate,
                                                              radius * 2.0, radius * 2.0)
    map.setRegion(coordinateRegion, animated: true)
}

I want to allow user only to zoom in/zoom out to the exact gps coordinates.

Currently user cannot scroll the map, but he can rotate it, also - he can double tap wherever on the map and it will zoom there.

I want to disable those options and make an effect, that when user double taps the map (or pinch) - it will only zoom to the initial location.

How can I do it, and also - why does the rotateEnabled = false doesn't work?

like image 976
user3766930 Avatar asked Oct 19 '22 00:10

user3766930


1 Answers

Add below code in double tap action method

var annotationPoint = MKMapPointForCoordinate(zoomToLocation.coordinate)
var zoomRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1)
mapView.setVisibleMapRect(zoomRect, animated: true)

Here zoomToLocation is your desired CLLocation and mapView is MKMapView's object.

Hope this will solve your problem :)

like image 155
Steve Gear Avatar answered Oct 21 '22 03:10

Steve Gear