2016-12-16 32 views
0

我試圖記錄用戶使用GoogleMap SDK的路徑。我正在使用多段線在地圖上繪製線條以向用戶顯示拍攝的路徑。在嘗試減少生成的座標並使行看起來乾淨(而不是看起來很花哨)時,我調用CLLocationManger的startMonitoringSignificantChange而不是startUpdatingLocation。但是,這似乎並不奏效。它在加載視圖時只調用一次didUpdateLocations方法,但在此之後,它只會停止調用。我在哪裏做錯了?startMonitoringSignificantLocationChange不會調用didUpdateLocations

這是我的代碼後locationManager.requestAlwaysAuthorization()

locationManager.allowsBackgroundLocationUpdates = true 
locationManager.pausesLocationUpdatesAutomatically = false 

附:以下代碼行

override func viewDidLoad() { 
     super.viewDidLoad() 
     locationManager.delegate = self 
     locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters 
     locationManager.requestAlwaysAuthorization() 

     locationManager.startMonitoringSignificantLocationChanges() 

    } 

    extension LocationViewController: CLLocationManagerDelegate { 

     func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) { 
      if status == .AuthorizedAlways { 
       locationManager.startMonitoringSignificantLocationChanges() 
       mapView.myLocationEnabled = true 
       mapView.settings.myLocationButton = true 
      } 
     } 

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

      if let location = manager.location { 

        path.addCoordinate(CLLocationCoordinate2D(latitude: location.coordinate.latitude, 
         longitude: location.coordinate.longitude)) 
        let polyline = GMSPolyline(path: path) 
        polyline.strokeColor = UIColor.redColor() 
        polyline.strokeWidth = 3 
        polyline.geodesic = true 
        polyline.map = mapView 

        // Save the coordinates to array 
        coordinateArray.append([location.coordinate.latitude, location.coordinate.longitude]) 



       } 


      } 

     } 
    } 
+0

當您期待更新而未獲得更新時,您移動了多遠? – dan

+0

去5公里以外的市中心。 – user30646

+0

您應該期望每隔500米更新一次。你是否將應用保持在前臺並且設備保持清醒狀態?您尚未啓用後臺位置信息,因此,如果您的應用已被暫停或設備處於睡眠狀態,您將無法獲得更新。 – Paulw11

回答

0

添加如果您的視圖持有locationManager實例,則在您的視圖未加載後,它可能不會收到位置更新。您可能會刪除以下行,因爲它對重要位置更新沒有用處:

locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters 
相關問題