在viewDidLoad()中使用CLLocationManager()可防止拖动或缩放MKMapView

时间:2017-07-26 19:43:09

标签: ios swift xcode

使用以下代码拒绝拖放地放大/缩小地图视图。

我尝试了很多事情。我已经在属性检查器中允许了所有必需的属性,甚至尝试添加isZoomEnabled和其他属性但没有运气。

import UIKit
import CoreLocation
import MapKit

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

    @IBOutlet var latitudeLabel: UILabel!
    @IBOutlet var longitudeLabel: UILabel!
    @IBOutlet var courseLabel: UILabel!
    @IBOutlet var speedLabel: UILabel!
    @IBOutlet var altitudeLabel: UILabel!
    @IBOutlet var addressLabel: UILabel!
    @IBOutlet var mapView: MKMapView!

    var manager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBest
        manager.requestWhenInUseAuthorization()
        manager.startUpdatingLocation()
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

        let location = locations[0]

        self.latitudeLabel.text = String(location.coordinate.latitude)
        self.longitudeLabel.text = String(location.coordinate.longitude)
        self.courseLabel.text = String(location.course)
        self.speedLabel.text = String(location.speed)
        self.altitudeLabel.text = String(location.altitude)

        let latitude = location.coordinate.latitude
        let longitude = location.coordinate.longitude
        let latDelta: CLLocationDegrees = 0.10
        let longDelta: CLLocationDegrees = 0.10
        let span = MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: longDelta)
        let coordinates = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
        let region = MKCoordinateRegion(center: coordinates, span: span)

        self.mapView.setRegion(region, animated: true)

        CLGeocoder().reverseGeocodeLocation(location) { (placemarks, error) in
            if error != nil {
                print(error!)
            }

            else {
                if let placemark = placemarks?[0] {
                    var address = ""

                    if placemark.subThoroughfare != nil {
                        address += placemark.subThoroughfare! + " "
                    }

                    if placemark.thoroughfare != nil {
                        address += placemark.thoroughfare! + "\n"
                    }

                    if placemark.subLocality != nil {
                        address += placemark.subLocality! + "\n"
                    }

                    if placemark.subAdministrativeArea != nil {
                        address += placemark.subAdministrativeArea! + "\n"
                    }

                    if placemark.postalCode != nil {
                        address += placemark.postalCode! + "\n"
                    }

                    if placemark.country != nil {
                        address += placemark.country! + " "
                    }

                    self.addressLabel.text = address

                }
            }
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

此代码应该放在哪里或者可以编辑什么来修复此问题?

1 个答案:

答案 0 :(得分:0)

每次更新地点时都会设置地图视图的区域,从而阻止您以有意义的方式与地图进行互动。

防止出现这种情况的一种方法是设置Bool,表示该区域已设置,并且不再设置该区域。

var regionHasBeenSet = false

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    // ...

    if !regionHasBeenSet {
        let latitude = location.coordinate.latitude
        let longitude = location.coordinate.longitude
        let latDelta: CLLocationDegrees = 0.10
        let longDelta: CLLocationDegrees = 0.10
        let span = MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: longDelta)
        let coordinates = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
        let region = MKCoordinateRegion(center: coordinates, span: span)

        self.mapView.setRegion(region, animated: true)

        regionHasBeenSet = true
    }

    CLGeocoder().reverseGeocodeLocation(location) { (placemarks, error) in
        // ...
    }
}

如果您希望地图的区域再次位于该位置的中心位置,只需将regionHasBeenSet更改为false,只要在下一个位置更新,该区域就会重置。