2016-07-27 55 views
0

我有這樣的代碼:獲取我的位置與谷歌地圖迅速

override func viewDidLoad() { 
    super.viewDidLoad() 

    locationManager.delegate = self 
    locationManager.requestWhenInUseAuthorization() 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest 
    locationManager.startUpdatingLocation() 

} 

和驗證碼:

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    let userLocation = locations.last 
    print(userLocation) 
    let camera = GMSCameraPosition.cameraWithLatitude(userLocation!.coordinate.latitude, 
                 longitude: userLocation!.coordinate.longitude, zoom: 12.5) 

    self.mapView.camera = camera 
    self.mapView.myLocationEnabled = true 
    // self.mapView.settings.myLocationButton = true 

    locationManager.stopUpdatingLocation() 
} 

這是較早的工作,但現在看來似乎是的LocationManager甚至沒有被因爲print語句沒有返回任何信息。代碼中沒有錯誤,我似乎無法弄清楚爲什麼函數沒有被調用。任何人都可以發現任何明顯的我失蹤的東西?

謝謝

+0

選中此問題[SO問題](http://stackoverflow.com/questions/26192480/cannot-get-my-location-to-show-up-in-go-ogle-maps-in-ios-8?rq= ),如果它可以幫助你。 – KENdi

+0

@KENdi不幸的是,它不是我想的那個問題。出於某種原因,該函數並沒有被調用。任何想法? – tryingtolearn

回答

0

我只是實現這一點,我會說,你我做了什麼

首先,創建一個可變的LocationManager

let locationManager: CLLocationManager = CLLocationManager()

你必須請求權限viewDidLoad()

viewMap.delegate = self 
locationManager.delegate = self as? CLLocationManagerDelegate 
locationManager.requestWhenInUseAuthorization() 

然後,我創建了一個擴展,我實現了委託

extension MyView: CLLocationManagerDelegate { 

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { 
    //here you configure the locationManager 

    locationManager.startUpdatingLocation() 

    viewMap.isMyLocationEnabled = true 
    viewMap.settings.myLocationButton = true 
} 

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

    } 

} 

我希望這可以幫助您,在這裏工作正常。