无法在地图视图中快速移动

Posted

技术标签:

【中文标题】无法在地图视图中快速移动【英文标题】: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)
    
        

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

【问题讨论】:

【参考方案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

【讨论】:

以上是关于无法在地图视图中快速移动的主要内容,如果未能解决你的问题,请参考以下文章

无法快速显示注释的左侧附件视图

Swiftui + 核心位置:地图无法以用户为中心

无法快速显示第一个视图控制器将值传递给第二个视图控制器

谷歌地图移动视图

从弹出视图快速移动到根 ViewController

用户位置的自定义注释视图不移动地图视图