簡體   English   中英

如何使用Swift 4 iOS在特定時間間隔內更新用戶的后台位置?

[英]How to update background location of user within specific time interval using swift 4 iOS?

我正在開發一個應用程序。 我正在更新用戶的背景位置。 並且我想在特定時間間隔內更新該位置。 我搜了很多。 但沒有任何解決方案。 請幫幫我。 下面是我的代碼。 提前致謝。

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
    {
        let userLocation :CLLocation = locations[0] as CLLocation
        print("user latitude = \(userLocation.coordinate.latitude)")
        print("user longitude = \(userLocation.coordinate.longitude)")

        self.labelLat.text = "\(userLocation.coordinate.latitude)"
        self.labelLongi.text = "\(userLocation.coordinate.longitude)"
        DataBaseFunctions.sharedInstance.saveUserDetail(dataToInsert: userLocation)
    }

這是在Swift-NOW中,您可以將Timer用於didUpdateLocation與

locationManager = CLLocationManager()
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
locationManager.delegate = self
locationManager.startUpdatingLocation()
var timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.updateLocation), userInfo: nil, repeats: true)

func updateLocation() {
    let location: CLLocation = locationManager.location
    // Your code
}

//標記:-CLLocationManagerDelegate

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    // Your code
}

暫無
暫無

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

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