Issue on GPS accuracy with locationListener

Hi everybody,
I have an issue with my program. I used the example program. When I do not move, the program is running correctly and accuracy is around 15-20 meters.
When I start to move (car trip), accuracy start to increase up to around 2000 m. And location is not updated so often.

I certainly make a mistake somewhere but do not know where. Can you help me ?

PS: I am using a Samsung Galaxy S9.

Kind regards,
Mikaël BOCKSTAL

<// Import needed Android libs
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.Manifest;

final float posMapsX = 1630; // 1630; TOTAL Width = 2220
final float posMapsY = 500; //500; TOTAL Height = 1080

// Set up the variables for the LocationManager and LocationListener
LocationManager locationManager;
MyLocationListener locationListener;

// Variables to manage GPS
boolean hasLocation = false;

void setup() {
fullScreen();
orientation(LANDSCAPE);

//Polices d’écritures
textFont(createFont(“SansSerif”, 20 * displayDensity)); //26
textAlign(CENTER, CENTER);
noStroke();
fill(0);

//Gestion GPS
requestPermission(“android.permission.ACCESS_FINE_LOCATION”, “initLocation”);

}

void draw() {
background(204); //204 = gris; 0 = noir

textAlign(LEFT, UP);
text("W " + width + “\n” + "H " + height+ “\n”, 10, 10, width, height);

if (mousePressed) {
if (mouseX < width/2) {
rect(0, 0, width/2, height); // Left
} else {
rect(width/2, 0, width/2, height); // Right
}
}

textAlign(CENTER, CENTER);

stroke(0); strokeWeight(3);
line(posMapsX-30, 0, posMapsX-30, height);
noStroke();

//Affichage des données GPS
if (hasPermission(“android.permission.ACCESS_FINE_LOCATION”) && hasLocation) {
textAlign(LEFT, UP);
text(locationListener.getCurrentLatitude() + “\n” +
locationListener.getCurrentLongitude() + “\n” +
“h: " + round((float)locationListener.getCurrentAltitude()) + " m” + “\n” +
“P: " + round((float)locationListener.getCurrentAccuracy()) + " m” + “\n” +
locationListener.getSpeedKMH() + " km/h" + “\n” +
"Prov: " + locationListener.getCurrentProvider(), posMapsX, 15, width, height);
} else {
text(“No permissions to access location”, 0, 0, width, height);
}
textAlign(CENTER, CENTER);
}

void initLocation(boolean granted) {
if (granted) {
Context context = getContext();
locationListener = new MyLocationListener();
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
hasLocation = true;
} else {
hasLocation = false;
}
}/>

<// Class for capturing the GPS data
class MyLocationListener implements LocationListener {
// Variables to hold the current and previous GPS data
double currentLatitude = 0; double currentLongitude = 0;
double currentAltitude = 0; float currentAccuracy = 0;
float currentSpeed = 0;
String currentProvider = “”;

public void onLocationChanged(Location location) {
currentLatitude = location.getLatitude();
currentLongitude = location.getLongitude();
currentAltitude = location.getAltitude();
currentAccuracy = location.getAccuracy();
currentSpeed = location.getSpeed();
currentProvider = location.getProvider();
}
public void onProviderDisabled (String provider) {
currentProvider = “”;
}
public void onProviderEnabled (String provider) {
currentProvider = provider;
}
public void onStatusChanged (String provider, int status, Bundle extras) {
}
public int getSpeedMS () { // “m/s”
return (int)currentSpeed;
}
public int getSpeedKMH () { // “km/h”
return (int)(currentSpeed/1000*3600);
}
public String getCurrentLatitude() {
if (currentLatitude > 0) {
return “N” + currentLatitude;
} else {
return “S” + currentLatitude;
}
}
public String getCurrentLongitude() {
if (currentLongitude > 0) {
return “E” + currentLongitude;
} else {
return “W” + currentLongitude;
}
}
public double getCurrentAltitude() {
return currentAltitude;
}
public float getCurrentAccuracy() {
return currentAccuracy;
}
public String getCurrentProvider() {
return currentProvider;
}
}/>

@PlatipuS ===
the way you initialize the locationUpdates is not correct if you are running or walking; it needs 2 parameters you set to 0, first one is for the period (in milliseconds), second one is for distance (meters): try to modify these params&& see the result. Other point could be that you are using networkprovider; try to use also GPS provider.

Hi akenaton,
Thank you. I used “locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 1, locationListener);” and now it is working fine.
Mikaël

@PlatipuS === ok, put answered for others