Skip to content

Commit

Permalink
#83 - [ETA] optimizeDistanceRequest - Distance value is not the good one
Browse files Browse the repository at this point in the history
Merge pull request #84 from Woosmap/forceRefreshETA_Leo
  • Loading branch information
lpernelle-woosmap authored Jun 28, 2022
2 parents 10d62cd + 7cea0e3 commit d7e01d5
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ public class Distance: Object {
@objc public dynamic var units: String?
@objc public dynamic var routing: String?
@objc public dynamic var status: String?
@objc public dynamic var locationId: String?


convenience public init(originLatitude: Double, originLongitude: Double, destinationLatitude: Double, destinationLongitude: Double,dateCaptured: Date, distance: Int, duration: Int, mode: String, units: String, routing: String, status: String) {

convenience public init(originLatitude: Double, originLongitude: Double, destinationLatitude: Double, destinationLongitude: Double,dateCaptured: Date, distance: Int, duration: Int, mode: String, units: String, routing: String, status: String, locationId: String) {
self.init()
self.originLatitude = originLatitude
self.originLongitude = originLongitude
Expand All @@ -37,6 +39,7 @@ public class Distance: Object {
self.units = units
self.routing = routing
self.status = status
self.locationId = locationId
}

}
Expand Down Expand Up @@ -86,6 +89,7 @@ public class Distances {
distance.duration = durationValue
distance.durationText = durationText
distance.status = element.status
distance.locationId = locationId
distanceArray.append(distance)
//}
indexElement+=1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ public class Locations {
}
return []
}

public class func getLocationFromId(id: String) -> Location? {
do {
let realm = try Realm()
let predicate = NSPredicate(format: "locationId == %@", id)
let fetchedResults = realm.objects(Location.self).filter(predicate)
if let aLocation = fetchedResults.last {
return aLocation
}
} catch {
}
return nil
}

public class func deleteAll() {
do {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public class RegionIsochrones {
regionToUpdate.didEnter = didEnter
regionToUpdate.expectedAverageSpeed = averageSpeed
regionToUpdate.date = Date()
regionToUpdate.locationId = distanceInfo.locationId
realm.add(regionToUpdate)
try realm.commitWrite()
return regionToUpdate
Expand Down
19 changes: 15 additions & 4 deletions WoosmapGeofencing/Sources/WoosmapGeofencing/Monitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,6 @@ public class LocationService: NSObject, CLLocationManagerDelegate {
regionIso.radius = radius
regionIso.type = "isochrone"
RegionIsochrones.add(regionIsochrone: regionIso)
if self.currentLocation != nil {
calculateDistanceWithRegion(location: self.currentLocation!)
}

return true
}
Expand Down Expand Up @@ -739,6 +736,10 @@ public class LocationService: NSObject, CLLocationManagerDelegate {
let regionsIsochrones = RegionIsochrones.getAll()
var regionsBeUpdated = false
for regionIso in regionsIsochrones {
if regionIso.locationId == nil
{
regionsBeUpdated = true
}
let distance = location.distance(from: CLLocation(latitude: regionIso.latitude,
longitude: regionIso.longitude))
if (distance < Double(distanceMaxAirDistanceFilter)) {
Expand All @@ -751,8 +752,18 @@ public class LocationService: NSObject, CLLocationManagerDelegate {
}
else{
if (!optimizeDistanceRequest){
var distanceFromTheLastRefresh = Double(0)
if let regionIsoLocationId = regionIso.locationId
{
if let locationFromTheLastRefresh = Locations.getLocationFromId(id: regionIsoLocationId)
{
distanceFromTheLastRefresh = location.distance(from: CLLocation(
latitude: locationFromTheLastRefresh.latitude,
longitude: locationFromTheLastRefresh.longitude))
}
}
if (spendtime > 60){ //1 minute
let averageSpeed:Double = distance/spendtime
let averageSpeed:Double = distanceFromTheLastRefresh/spendtime
let averageSpeedLimit:Double = regionIso.expectedAverageSpeed * 2
if(averageSpeed > averageSpeedLimit){
regionsBeUpdated = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import RealmSwift
}

private func initRealm() {
Realm.Configuration.defaultConfiguration = Realm.Configuration(schemaVersion: 7)
Realm.Configuration.defaultConfiguration = Realm.Configuration(schemaVersion: 8)
}

public func getLocationService() -> LocationService {
Expand Down

0 comments on commit d7e01d5

Please sign in to comment.