更新 userLocation 更改的时间戳 swift
Updating timestamp on userLocation change swift
我这里有一个应用程序,我获取用户位置并获取时间戳。我现在遇到的问题是,时间戳继续更新,但我希望它仅在位置坐标发生变化时更新。
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))"
}
将根据要求提供更多代码
好吧,在为 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
我这里有一个应用程序,我获取用户位置并获取时间戳。我现在遇到的问题是,时间戳继续更新,但我希望它仅在位置坐标发生变化时更新。
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))"
}
将根据要求提供更多代码
好吧,在为 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