I’ve this:
public class GpsUpdate extends Observable implements LocationListener {
private int x_position;
private int y_position;
private float currentAccuracy;
public GpsUpdate(Observer observer){
addObserver(observer);
}
@Override
public void onLocationChanged(Location location) {
int lat = (int) (location.getLatitude() * 1E6);
int lng = (int) (location.getLongitude() * 1E6);
setAccuracy(location.getAccuracy());
setX_position(lat);
setY_position(lng);
setChanged();
notifyObservers();
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public int getX_position() {
return x_position;
}
public void setX_position(int x_position) {
this.x_position = x_position;
}
public int getY_position() {
return y_position;
}
public void setY_position(int y_position) {
this.y_position = y_position;
}
public void setAccuracy(float currentAccuracy) {
this.currentAccuracy = currentAccuracy;
}
public float getAccuracy(){
return currentAccuracy;
}
}
this method (onLocationChanged()) notifies another method (update()) in other class that position is changed, but I need to collect all coordinates even that the current is the same like previous.
_GPSUPDATE = new GpsUpdate(this);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, _GPSUPDATE);
@Override
public void update(Observable observable, Object data) {
xCurrent = _GPSUPDATE.getX_position();
yCurrent = _GPSUPDATE.getY_position();
.....
}
So, what have I to add/change to get position every second?
This is rather bad approach. Even if you need to store location every second then redefine your app logic to store timestamped location. This suffices because (let’s assume
Tis delta time in seconds) if atT=0you received and storedlocationA, atT=2you received and storedlocationBthen if you want to know what location was atT=1then not seeing explicit record you know there was NO CHANGE of location at that time, so it is clear your location equals to last stored one with closest lowerT, in this case atT=1your location (still) waslocationAas it did NOT change sinceT=0. This is much better approach and would save you hell lot of storage. And if you store this in datbase then getting right results is trivial as well – just search for entry with timestamp less or equal to required, order by timestamp desc, limit 1.