【问题标题】:Can't move in map view swift无法在地图视图中快速移动
【发布时间】:2016-12-15 16:47:00
【问题描述】:

我无法将地图移动或缩放到其他位置。我使用本教程:https://www.youtube.com/watch?v=qrdIL44T6FQ 在地图中显示我的位置。出于某种原因,地图在打开应用程序后首先会非常缓慢(+- 30 秒)放大到我的位置。然后,当我想在地图中移动时,我不能。它停留在我当前的位置。

这是我的代码:

import UIKit
import MapKit
import CoreLocation

class FirstViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

    @IBOutlet weak var mapView: MKMapView!
    
    let locationManager = CLLocationManager()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.locationManager.delegate = self
        
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        
        self.locationManager.requestWhenInUseAuthorization()
        
        self.locationManager.startUpdatingLocation()
        
        self.mapView.showsUserLocation = true
        
        


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    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.5, longitudeDelta: 0.5 ))
        
        self.mapView.setRegion(region, animated: true)
        
        self.locationManager.startUpdatingLocation()
    
    }
    
    func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
        print("Errors: " + error.localizedDescription)
    }
        

}

有人知道这个问题吗?谢谢!

【问题讨论】:

    标签: swift dictionary view location


    【解决方案1】:

    没关系,我在某个地方犯了错误。此代码运行良好:

    import UIKit
    import MapKit
    import CoreLocation
    
    class FirstViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
    
        @IBOutlet weak var mapView: MKMapView!
        
        let locationManager = CLLocationManager()
        
        override func viewDidLoad()
        {
            super.viewDidLoad()
            
            self.locationManager.delegate = self
            self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
            self.locationManager.requestWhenInUseAuthorization()
            self.locationManager.startUpdatingLocation()
            
            self.mapView.showsUserLocation = true
            
        }
        
        override func didReceiveMemoryWarning()
        {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
        
        // MARK: - Location Delegate Methods
        
        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: 1, longitudeDelta: 1))
            
            self.mapView.setRegion(region, animated: true)
            
            self.locationManager.stopUpdatingLocation()
        }
        
        func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
        {
            print("Error: " + error.localizedDescription)
        }
        
    }

    【讨论】:

      【解决方案2】:

      对于那些想知道他的错误是什么的人:

      他将self.locationManager.startUpdatingLocation() 放在locationManager 方法中,而不是self.locationManager.stopUpdatingLocation

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-10-08
        • 2016-05-07
        • 1970-01-01
        • 2014-10-31
        • 1970-01-01
        • 2015-06-09
        • 2016-12-16
        • 2016-11-04
        相关资源
        最近更新 更多