Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private boolean filterAndAddLocation(Location location){
- long age = getLocationAge(location);
- if(age > 10 * 1000){ //more than 10 seconds
- Log.d(TAG, "Location is old");
- oldLocationList.add(location);
- return false;
- }
- if(location.getAccuracy() < 0){
- Log.d(TAG, "Latitidue and longitude values are invalid.");
- noAccuracyLocationList.add(location);
- return false;
- }
- //setAccuracy(newLocation.getAccuracy());
- float horizontalAccuracy = location.getAccuracy();
- if(horizontalAccuracy > 100){
- Log.d(TAG, "Accuracy is too low.");
- inaccurateLocationList.add(location);
- return false;
- }
- /* Kalman Filter */
- float Qvalue;
- long locationTimeInMillis = (long)(location.getElapsedRealtimeNanos() / 1000000);
- long elapsedTimeInMillis = locationTimeInMillis - runStartTimeInMillis;
- if(currentSpeed == 0.0f){
- Qvalue = 3.0f; //3 meters per second
- }else{
- Qvalue = currentSpeed; // meters per second
- }
- kalmanFilter.Process(location.getLatitude(), location.getLongitude(), location.getAccuracy(), elapsedTimeInMillis, Qvalue);
- double predictedLat = kalmanFilter.get_lat();
- double predictedLng = kalmanFilter.get_lng();
- Location predictedLocation = new Location("");//provider name is unecessary
- predictedLocation.setLatitude(predictedLat);//your coords of course
- predictedLocation.setLongitude(predictedLng);
- float predictedDeltaInMeters = predictedLocation.distanceTo(location);
- if(predictedDeltaInMeters > 60){
- Log.d(TAG, "Kalman Filter detects mal GPS, we should probably remove this from track");
- kalmanFilter.consecutiveRejectCount += 1;
- if(kalmanFilter.consecutiveRejectCount > 3){
- kalmanFilter = new KalmanLatLong(3); //reset Kalman Filter if it rejects more than 3 times in raw.
- }
- kalmanNGLocationList.add(location);
- return false;
- }else{
- kalmanFilter.consecutiveRejectCount = 0;
- }
- /* Notifiy predicted location to UI */
- Intent intent = new Intent("PredictLocation");
- intent.putExtra("location", predictedLocation);
- LocalBroadcastManager.getInstance(this.getApplication()).sendBroadcast(intent);
- Log.d(TAG, "Location quality is good enough.");
- currentSpeed = location.getSpeed();
- locationList.add(location);
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement