簡體   English   中英

Google Maps iOS SDK防止每次位置更新后相機變焦

[英]Google Maps iOS SDK Prevent camera zoom after each location update

我有一個應用程序,需要不斷更新用戶的位置,以便顯示他們當前的坐標和海拔。 我正在使用didUpdateLocations函數進行此操作:

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和其他幾個地方,但這導致應用程序崩潰,因為無法及時找到用戶。

有人對如何進行這項工作有任何建議嗎? 謝謝。

關於實現位置更新,GitHub中發布了一個問題-由於位置更新優化並經過了線程,因此實現了響應式用戶位置跟蹤模式 ,一種給定的變通方法是在地圖上顯示用戶位置

nMapView.setMyLocationEnabled(true);

代替:

nMapView.setMyLocationTrackingMode(MyLocationTrackingMode.TRACKING_FOLLOW);

然后,關於相機縮放,如“ 相機和視圖-縮放”中所述 ,您可以嘗試設置最小或最大縮放以限制縮放級別。 就像聲明的那樣,

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

您也可以嘗試在此SO后Google Maps iOS SDK中獲取解決方案,獲取用戶的當前位置 希望能幫助到你。

用它代替那條線(mapview.camera = ...)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM