更新userLocation更改时间戳

时间:2019-02-20 12:00:21

标签: swift core-location

我在这里有一个应用程序,可以获取用户位置并获取时间戳。我现在遇到的问题是,时间戳记会继续更新,但我希望仅在位置坐标更改时才更新。

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

        getLocation(location: locations.last?.coordinate)
}



func getLocation(location manager: CLLocationCoordinate2D) {

            let loc = CLLocation(latitude: manager.latitude, longitude: manager.longitude)
            let geoTag = GeoTag()
            let coordinates = Coordinate()
            coordinates.accuracy = Double(loc.horizontalAccuracy).rounded(toPlaces: 3)
            coordinates.altitude = Double(loc.altitude).rounded(toPlaces: 7)
            coordinates.heading = 0.0
            coordinates.lat = Double(loc.coordinate.latitude).rounded(toPlaces: 7)
            coordinates.lng = Double(loc.coordinate.longitude).rounded(toPlaces: 7)
            coordinates.speed = loc.speed

            geoTag.coordinates = coordinates
            geoTag.timestamp = "\(Double(loc.timestamp.timeIntervalSince1970).rounded(toPlaces: 0) * 1000))"

    }

更多代码将应要求提供

1 个答案:

答案 0 :(得分:0)

在为locationManager设置委托时,您也应该添加以下行

    locationManager.startMonitoringSignificantLocationChanges() //this will call //didUpdateLocations after a significant change in location

locationManager.distanceFilter = 20  // replace it with what distance in meters you want //this will call you didUpdateLocations method after you travel 20 meters