如何在 Swift 中检测缩放对地图的影响

Posted

技术标签:

【中文标题】如何在 Swift 中检测缩放对地图的影响【英文标题】:How to detect zoom's effect on the map in Swift 【发布时间】:2016-08-13 07:38:02 【问题描述】:

我将regionDidChangeAnimated 用于我的mapView。我想检测用户何时缩放或拖动地图。正在检测拖动,但我似乎无法检测到用户何时缩放地图。我尝试使用:

func mapView(mapView: MKMapView, regionWillChangeAnimated animated: Bool) 
    print("region will change")

但是当我缩放我的地图时它不起作用。

完整代码:

var locationManager: CLLocationManager = CLLocationManager()
var startLocation: CLLocation!

@IBOutlet weak var mapView: MKMapView!

override func viewDidLoad() 
    super.viewDidLoad()

    //get current location
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.delegate = self
    locationManager.requestWhenInUseAuthorization()
    locationManager.startUpdatingLocation()

    self.mapView.showsUserLocation = true
    startLocation = nil

    let mapDragRecognizer = UIPanGestureRecognizer(target: self, action: "didDragMap:")
    mapDragRecognizer.delegate = self
    self.mapView.addGestureRecognizer(mapDragRecognizer)
    self.mapView.pitchEnabled = true
    self.mapView.showsBuildings = true



func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [CLLocation]) 
    let location = locations.last
    let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05))
    mapView.setRegion(region, animated: false)
    let currentLocX = String(format: "%.4f", location!.coordinate.latitude)
    let currentLocY = String(format: "%.4f", location!.coordinate.longitude)

    self.locationManager.stopUpdatingLocation()


func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool 
    return true


func locationManager(manager: CLLocationManager, didFailWithError error: NSError) 
    print("Errors: " + error.localizedDescription)


func didDragMap(gestureRecognizer: UIGestureRecognizer) 
    print("Drag")


func mapView(mapView: MKMapView, regionWillChangeAnimated animated: Bool) 
    print("Zoom")

【问题讨论】:

【参考方案1】:

我已经解决了。在override func viewDidLoad()需要加self.mapView.delegate = self 并添加用于确定地图缩放的功能:

private var mapChangedFromUserInteraction = false

private func mapViewRegionDidChangeFromUserInteraction() -> Bool 
    let view = self.mapView.subviews[0]
    if let gestureRecognizers = view.gestureRecognizers 
        for recognizer in gestureRecognizers 
            if( recognizer.state == UIGestureRecognizer.State.began || recognizer.state == UIGestureRecognizer.State.ended )) 
                return true
            
        
    
    return false


func mapView(_ mapView: MKMapView, regionWillChangeAnimated animated: Bool) 
    mapChangedFromUserInteraction = mapViewRegionDidChangeFromUserInteraction()



func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) 
    if (mapChangedFromUserInteraction) 
        print("ZOOM finished")
    

【讨论】:

以上是关于如何在 Swift 中检测缩放对地图的影响的主要内容,如果未能解决你的问题,请参考以下文章

如何忽略缩放图像视图 Swift 3

如何在不更改用户设置的缩放比例的情况下更新地图标记?

Swift 2 MapKit Annotations - 如何对注释进行分组?

supermap如何修改自带的缩放等级

如何在 Swift 中检测对 UITableViewCell 的 UIImageView 的触摸?

Leaflet.js:检测地图何时完成缩放