有什么方法可以将MKMapView仅缩放到特定区域,同时禁用任何其他用户交互? [英] is there any way of allowing zooming MKMapView only to a specific region, while disabling any other user interaction?
问题描述
我有一个带有MapView的swift
应用程序.这些是我在地图故事板上的设置:
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
我的方法centerMapOnLocation
首先将地图居中放置在特定位置:
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)
}
我只允许用户放大/缩小到确切的gps坐标.
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.
我该怎么办?-rotateEnabled = false
为什么不起作用?
How can I do it, and also - why does the rotateEnabled = false
doesn't work?
推荐答案
在双击操作方法中添加以下代码
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)
在这里zoomToLocation是所需的CLLocation,而mapView是MKMapView的对象.
Here zoomToLocation is your desired CLLocation and mapView is MKMapView's object.
希望这可以解决您的问题:)
Hope this will solve your problem :)
这篇关于有什么方法可以将MKMapView仅缩放到特定区域,同时禁用任何其他用户交互?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!