我試圖在Swift 2中設置我的地圖上的最小縮放級別。我找不到任何有關如何限制地圖放大得太遠的文檔我決定嘗試的是監視地圖移動(例如拖動或縮放),然後將MKZoomScale
設置回最小值。如何檢測mapView在Swift中移動並更新縮放
我發現的regionDidChangeAnimated
的大部分答案都在Objective C中,我不知道,但我很難將它們轉換爲Swift。
我嘗試實現@ hEADcRASH的回答:https://stackoverflow.com/a/30924768/4106552,但是當地圖在模擬器中移動時,它不會觸發並將任何內容打印到控制檯。
誰能告訴我我做錯了什麼?我是Swift新手,所以這可能是一個小錯誤。此外,讓我知道是否有一個輕量級的方法來解決限制地圖上的縮放級別。我擔心移動顯示器會讓地圖動畫變慢一點。謝謝您的幫助。
這是我的視圖控制器。 進口的UIKit 進口解析 進口MapKit
class SearchRadiusViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
@IBOutlet weak var map: MKMapView!
@IBOutlet weak var menuBtn: UIBarButtonItem!
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
//menu button control
if self.revealViewController() != nil {
menuBtn.target = self.revealViewController()
menuBtn.action = "revealToggle:"
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
//user location
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
//set map
let location:CLLocationCoordinate2D = manager.location!.coordinate
let latitude = location.latitude
let longitude = location.longitude
let latDelta:CLLocationDegrees = 0.1
let longDelta:CLLocationDegrees = 0.1
let span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
let maplocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
let region:MKCoordinateRegion = MKCoordinateRegionMake(maplocation, span)
map.setRegion(region, animated: true)
//stop updating location, only need user location once to position map.
manager.stopUpdatingLocation()
}
//Attempt to monitor for map movement based on hEADcRASH's answer.
private var mapChangedFromUserInteraction = false
private func mapViewRegionDidChangeFromUserInteraction() -> Bool {
let view = self.map.subviews[0]
// Look through gesture recognizers to determine whether this region change is from user interaction
if let gestureRecognizers = view.gestureRecognizers {
for recognizer in gestureRecognizers {
if(recognizer.state == UIGestureRecognizerState.Began || recognizer.state == UIGestureRecognizerState.Ended) {
return true
}
}
}
return false
}
func mapView(mapView: MKMapView, regionWillChangeAnimated animated: Bool) {
print("yes")
mapChangedFromUserInteraction = mapViewRegionDidChangeFromUserInteraction()
if (mapChangedFromUserInteraction) {
// user changed map region
print("user changed map in WILL")
}
}
func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
print("yes ddd")
if (mapChangedFromUserInteraction) {
// user changed map region
print("user changed map in Did")
}
}
}
您是否在故事板中設置mapView的委託?如果不是的話,你應該在viewDidLoad方法中添加self.map.delegate = self。 – lorenzoliveto
感謝@lorenzoliveto,將'self.map.delegate = self'添加到viewDidLoad正在工作。 mapViewRegionDidChangeAnimated正在工作!您是否有意見設置最大縮放級別的最佳方式?我應該走觀看地圖移動/縮放和重置地圖縮放的路線嗎? – tylerSF
是的,我認爲這是您擁有的唯一選擇。 regionWillChangeAnimated:在滾動過程中多次調用,而regionDidChangeAnimated:在滾動後僅調用一次。嘗試在兩者中添加重置代碼並查看性能如何受到影響。 – lorenzoliveto