From 3c5de007bf83d14a564c9b71e4c70f0e41b6c22d Mon Sep 17 00:00:00 2001 From: vng Date: Thu, 26 Mar 2015 00:44:16 +0300 Subject: [PATCH] [android, iOS] Equal logic for the LocationPredictor components. --- .../maps/location/LocationPredictor.java | 27 +++++++++++------ iphone/Maps/Classes/LocationPredictor.mm | 29 ++++++++++++------- 2 files changed, 36 insertions(+), 20 deletions(-) diff --git a/android/src/com/mapswithme/maps/location/LocationPredictor.java b/android/src/com/mapswithme/maps/location/LocationPredictor.java index 92290c534b..b6da1e5770 100644 --- a/android/src/com/mapswithme/maps/location/LocationPredictor.java +++ b/android/src/com/mapswithme/maps/location/LocationPredictor.java @@ -15,9 +15,8 @@ public class LocationPredictor private Handler mHandler; private LocationHelper.LocationListener mListener; - private Location mLastLocation; - private boolean mGeneratePredictions; - // TODO variable ISNT really changed anywhere. + private Location mLastLocation = null; + private boolean mGeneratePredictions = false; private int mPredictionCount; private int mConnectionSlot; @@ -82,32 +81,42 @@ public class LocationPredictor if (mode < LocationState.NOT_FOLLOW) mLastLocation = null; - mGeneratePredictions = mode == LocationState.ROTATE_AND_FOLLOW; + mGeneratePredictions = (mode == LocationState.ROTATE_AND_FOLLOW); resetHandler(); } + private boolean isPredict() + { + return mLastLocation != null && mGeneratePredictions; + } + private void resetHandler() { mPredictionCount = 0; + mHandler.removeCallbacks(mRunnable); - if (mLastLocation != null && mGeneratePredictions) + + if (isPredict()) mHandler.postDelayed(mRunnable, PREDICTION_INTERVAL); } private boolean generatePrediction() { - if (mLastLocation == null || !mGeneratePredictions) + if (!isPredict()) return false; if (mPredictionCount < MAX_PREDICTION_COUNT) { + ++mPredictionCount; + Location info = new Location(mLastLocation); info.setTime(System.currentTimeMillis()); - long elapsedMillis = info.getTime() - mLastLocation.getTime(); + final long elapsedMillis = info.getTime() - mLastLocation.getTime(); - double[] newLatLon = Framework.predictLocation(info.getLatitude(), info.getLongitude(), info.getAccuracy(), info.getBearing(), - info.getSpeed(), elapsedMillis / 1000.0); + double[] newLatLon = Framework.predictLocation(info.getLatitude(), info.getLongitude(), + info.getAccuracy(), info.getBearing(), + info.getSpeed(), elapsedMillis / 1000.0); info.setLatitude(newLatLon[0]); info.setLongitude(newLatLon[1]); diff --git a/iphone/Maps/Classes/LocationPredictor.mm b/iphone/Maps/Classes/LocationPredictor.mm index 5edca9e3ac..2c2e32999e 100644 --- a/iphone/Maps/Classes/LocationPredictor.mm +++ b/iphone/Maps/Classes/LocationPredictor.mm @@ -5,6 +5,7 @@ #include "../../../map/location_state.hpp" + namespace { NSTimeInterval const PREDICTION_INTERVAL = 0.2; // in seconds @@ -32,12 +33,13 @@ namespace m_timer = nil; m_gpsInfoIsValid = false; m_generatePredictions = false; - m_predictionCount = 0; - + m_connectionSlot = GetFramework().GetLocationState()->AddStateModeListener([self](location::State::Mode mode) { - m_generatePredictions = mode == location::State::RotateAndFollow; - m_gpsInfoIsValid = mode < location::State::NotFollow ? false : m_gpsInfoIsValid; + m_generatePredictions = (mode == location::State::RotateAndFollow); + if (mode < location::State::NotFollow) + m_gpsInfoIsValid = false; + [self resetTimer]; }); } @@ -60,22 +62,27 @@ namespace m_lastGpsInfo.m_source = location::EPredictor; } else - { m_gpsInfoIsValid = false; - } + [self resetTimer]; } +-(bool)isPredict +{ + return m_gpsInfoIsValid && m_generatePredictions; +} + -(void)resetTimer { m_predictionCount = 0; + if (m_timer != nil) { [m_timer invalidate]; m_timer = nil; } - if (m_gpsInfoIsValid && m_generatePredictions) + if ([self isPredict]) { m_timer = [NSTimer scheduledTimerWithTimeInterval:PREDICTION_INTERVAL target:self @@ -87,13 +94,13 @@ namespace -(void)timerFired { - if (!(m_gpsInfoIsValid && m_generatePredictions)) + if (![self isPredict]) return; - if (m_lastGpsInfo.HasBearing() && m_lastGpsInfo.HasSpeed() && m_predictionCount < MAX_PREDICTION_COUNT) + if (m_predictionCount < MAX_PREDICTION_COUNT) { ++m_predictionCount; - + location::GpsInfo info = m_lastGpsInfo; info.m_timestamp = my::Timer::LocalTime(); ::Framework::PredictLocation(info.m_latitude, info.m_longitude, info.m_horizontalAccuracy, info.m_bearing, @@ -103,4 +110,4 @@ namespace } } -@end \ No newline at end of file +@end