2016-08-03 37 views
0

我有一個應用程序需要不斷更新用戶位置,以便我可以顯示其當前座標和高度。我使用的didUpdateLocations功能這樣做:谷歌地圖iOS SDK每個位置更新後防止攝像頭縮放

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    if let location = locations.last { 
     mapView.camera = GMSCameraPosition(target: locationManager.location!.coordinate, zoom: 15, bearing: 0, viewingAngle: 0) 
     let locValue : CLLocationCoordinate2D = manager.location!.coordinate 
     let altitude : CLLocationDistance = Double(round(1000*manager.location!.altitude)/1000) 
     let long = Double(round(10000*locValue.longitude)/10000) 
     let lat = Double(round(10000*locValue.latitude)/10000) 
     let alt = String(altitude) + " m" 
     latitudeLabel.text = String(lat) 
     longitudeLabel.text = String(long) 
     altitudeLabel.text = alt 
     showLearningObjectsWithinRange(location) 
    } 
} 

的問題是,當我嘗試放大地圖上的某一點,如果我移動設備甚至略有鏡頭拉近回來了一次。很顯然,這是因爲我的didUpdateLocations函數中的第一行設置了攝像頭位置,但是如果我刪除該線,地圖根本不會居中它們的位置。

我試着將GMSCameraPosition代碼移動到viewDidLoad,viewWillAppear和其他幾個地方,但是這導致應用程序崩潰,因爲它無法及時找到用戶。

有沒有人有關於如何使這項工作的任何建議?謝謝。

+0

變焦這一點:15,使用map.camera.zoom東西或文檔檢查,使得變焦並沒有改變當你更新位置 – Shubhank

回答

0

至於執行位置更新,有在GitHub上發佈了一個問題 - Implement Responsive User Location Tracking Mode由於位置更新優化,並通過線去,給定的解決方法,在地圖上顯示用戶的位置是調用

nMapView.setMyLocationEnabled(true); 

代替:

nMapView.setMyLocationTrackingMode(MyLocationTrackingMode.TRACKING_FOLLOW); 

然後,關於相機的變焦,在Camera and View - Zoom討論,你可以嘗試設置最小或最大變焦限制縮放級別。如上所述,

您可以通過設置最小和最大縮放級別來限制可用於地圖的縮放範圍。

您也可以嘗試在SO帖子中給出的解決方案 - Google Maps iOS SDK, Getting Current Location of user。希望能幫助到你。

+0

這是無關緊要的。問題是關於谷歌地圖。 – user3687

0

使用的,而不是某些行(mapview.camera = ...)

mapView.animate(toLocation: CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)) 
+0

這不起作用 – user3687