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

十年热恋 提交于 2020-01-03 16:00:13

问题


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

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?


回答1:


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 :)



来源:https://stackoverflow.com/questions/40074084/is-there-any-way-of-allowing-zooming-mkmapview-only-to-a-specific-region-while

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!