2016-02-28 28 views
6

我正在編寫一個應用程序,並且我有一個嵌入的mapview來顯示用戶的位置。這是到目前爲止我的代碼:如何讓地圖在用戶位置上迅速居中?

class YourCurrentLocation: UIViewController, CLLocationManagerDelegate { 

    @IBOutlet weak var mapView: MKMapView! 

    var locationManager = CLLocationManager() 
    let regionRadius: CLLocationDistance = 1000 

    func checkLocationAuthorizationStatus() { 
     if CLLocationManager.authorizationStatus() == .AuthorizedWhenInUse { 
      mapView.showsUserLocation = true 
      centerMapOnLocation(locationManager.location!, map: mapView, radius: regionRadius) 
     } else { 
      locationManager.requestAlwaysAuthorization() //requestWhenInUseAuthorization() 
     } 
    } 



    func centerMapOnLocation(location: CLLocation, map: MKMapView, radius: CLLocationDistance) { 
     let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, 
      radius * 2.0, radius * 2.0) 
     map.setRegion(coordinateRegion, animated: true) 
    } 


    override func viewDidLoad() { 
     super.viewDidLoad() 

     // mapView.delegate = self 


     if CLLocationManager.locationServicesEnabled() 
     { 
      //locationManager = CLLocationManager() 
      locationManager.delegate = self 
      locationManager.requestAlwaysAuthorization() 
      locationManager.desiredAccuracy = kCLLocationAccuracyBest 
      locationManager.startUpdatingLocation() 
      print("location enabled") 
      checkLocationAuthorizationStatus()    
     } 
     else 
     { 
      print("Location service disabled"); 
     } 


     // Do any additional setup after loading the view. 
    } 

} 

我還添加了兩個條目,以我的plist:

NSLocationAlwaysUsageDescription 
NSLocationWhenInUseUsageDescription 

,並在我的Xcode我已經設置爲模擬GPS數據在英國倫敦。

當我運行應用程序 - 我看到地圖,但倫敦沒有標記。我究竟做錯了什麼?

順便說一句,我不得不註釋掉該行:

//mapView.delegate = self 
viewDidLoad()

,否則我有錯誤:

Cannot assign value of type YourCurrentLocation to type MKMapViewDelegate 

,我不知道這是問題的一部分這裏。

我希望在顯示給用戶地圖時顯示效果,並在地圖上標出與他的位置一致的點。你能幫助我嗎?

+0

''locationManager.location!'在你的代碼中會給你模擬器或設備的最後一個位置,這可能是任何東西。在我的回答中,我向你展示瞭如何使用'CLLocationManagerDelegate'來獲取實際的用戶位置。 – paulvs

+0

好的,我明白了......謝謝!最後一個問題 - 在手機不在GPS範圍內的情況下會發生什麼情況,或者該位置還沒有被識別爲真實的? – user3766930

+0

@ user3766930如果確實解決了您的問題,您可以將答案標記爲已接受。 – paulvs

回答

13

您的代碼存在的問題是,當用戶提供位置權限時,您正嘗試將地圖指向用戶的位置,因此您不會等待CoreLocation爲您提供實際的用戶位置。 您需要使用CLLocationManagerDelegate方法locationManager(_:didUpdateLocations:)獲得用戶的實際位置時的通知,並且您可以將地圖設置爲指向用戶的位置。

class ViewController: UIViewController, CLLocationManagerDelegate { 

    @IBOutlet var mapView: MKMapView! 
    var locationManager: CLLocationManager? 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     locationManager = CLLocationManager() 
     locationManager!.delegate = self 

     if CLLocationManager.authorizationStatus() == .AuthorizedWhenInUse { 
      locationManager!.startUpdatingLocation() 
     } else { 
      locationManager!.requestWhenInUseAuthorization() 
     } 
    } 

    func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) { 


     switch status { 
     case .NotDetermined: 
      print("NotDetermined") 
     case .Restricted: 
      print("Restricted") 
     case .Denied: 
      print("Denied") 
     case .AuthorizedAlways: 
      print("AuthorizedAlways") 
     case .AuthorizedWhenInUse: 
      print("AuthorizedWhenInUse") 
      locationManager!.startUpdatingLocation() 
     } 
    } 

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

     let location = locations.first! 
     let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, 500, 500) 
     mapView.setRegion(coordinateRegion, animated: true) 
     locationManager?.stopUpdatingLocation() 
     locationManager = nil 
    } 

    func locationManager(manager: CLLocationManager, didFailWithError error: NSError) { 
     print("Failed to initialize GPS: ", error.description) 
    } 
} 
+0

好的,我看到你修剪了我的代碼很多,是不是必要的代碼行的其餘部分? – user3766930

+0

順便說一句,有沒有在用戶的確切位置上顯示地圖上的紅色針的方法?到目前爲止,我只能看到確切的區域.. – user3766930

+1

設置'kCLLocationAccuracyBest'不是絕對必要的,因爲它是根據文檔的默認值,儘管設置它沒有任何傷害。我還更新了'CoreLocation'''authorizationStatus'的檢查。 – paulvs