2016-11-20 15 views
0

我有一個GMSMapView連接到我的mapView變量。它全部設置好,甚至爲用戶在地圖上放置一條隨機多段線。目前我有一個「導航」按鈕,但它所做的只是放大用戶。我想這樣做,一旦他們點擊「導航」,它將使相機跟隨用戶,而不是讓他們從他們的位置滾動(很像谷歌地圖應用程序)。我怎樣才能使這成爲可能?謝謝!如何讓我的Google地圖隨時關注用戶,而不是讓他們滾動

回答

1

我假設你已經得到了一個CLLocationManager並開始用它來更新用戶的位置。完成後,只要位置發生變化,就需要在CLLocationManager的委託方法中設置mapView的相機。

沿東西這行:

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    if let location = manager.location { 
     // Create camera with the user's new location 
     let camera = GMSCameraPosition.cameraWithLatitude(location.coordinates.latitude, longitude: location.coordinates.longitude, zoom: 17.0) 
     // Animate the map to the new camera position 
     mapView.animateToCameraPosition(camera) 
    } 
} 

變焦參數玩得到你想要的結果。

希望它有幫助。

+0

所以這個方法運行,只要它注意到用戶的位置已經改變?它只在「manager.startUpdatingLocation()」發生時才運行嗎?當「manager.stopUpdatingLocation()」被調用時它停止嗎? – skyleguy

+0

並且您認爲哪裏會是第一個打電話給startUpdatingLocation的好地方? – skyleguy

+0

@skyleguy是的。每當用戶的位置發生變化時,都會調用該特定的委託方法。 [Apple的文檔](https://developer.apple.com/reference/corelocation/cllocationmanagerdelegate)聲明,只要有新的位置數據可用,它就會被調用。這個_usually_意味着用戶的位置發生變化。當調用stopUpdatingLocation()時,委託方法將停止。 至於startUpdatingLocation的一個好地方,它很大程度上取決於你的用例。從你最初的問題來看,我認爲viewDidLoad可能是你做這件事的好地方。 –

相關問題