更新userLocation上的时间戳更改swift

问题描述 投票:-1回答:1

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

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))"

    }

将根据要求提供更多代码

swift core-location
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.