# Issue on GPS accuracy with locationListener

**URL:** <https://discourse.processing.org/t/issue-on-gps-accuracy-with-locationlistener/22332>\
**Category:** Processing for Android\
**Created:** [July 2, 2020, 6:35pm UTC](https://discourse.processing.org/t/issue-on-gps-accuracy-with-locationlistener/22332 "2020-07-02T18:35:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![PlatipuS](https://avatars.discourse-cdn.com/v4/letter/p/a698b9/32.png) [@PlatipuS](https://discourse.processing.org/u/PlatipuS)\
**Post date:** [July 2, 2020, 6:35pm UTC](https://discourse.processing.org/t/issue-on-gps-accuracy-with-locationlistener/22332/1 "2020-07-02T18:35:55Z")

</div>

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;  
}  
}/\>

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [July 3, 2020, 8:54am UTC](https://discourse.processing.org/t/issue-on-gps-accuracy-with-locationlistener/22332/2 "2020-07-03T08:54:28Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![PlatipuS](https://avatars.discourse-cdn.com/v4/letter/p/a698b9/32.png) [@PlatipuS](https://discourse.processing.org/u/PlatipuS)\
**Post date:** [July 4, 2020, 9:01pm UTC](https://discourse.processing.org/t/issue-on-gps-accuracy-with-locationlistener/22332/3 "2020-07-04T21:01:22Z")

</div>

> [@akenaton](#):
>
> 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

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [July 5, 2020, 7:31am UTC](https://discourse.processing.org/t/issue-on-gps-accuracy-with-locationlistener/22332/4 "2020-07-05T07:31:12Z")

</div>

@PlatipuS === ok, put answered for others

---

<div class="post-metadata">

**Author:** ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)\
**Post date:** [May 1, 2023, 8:23am UTC](https://discourse.processing.org/t/issue-on-gps-accuracy-with-locationlistener/22332/5 "2023-05-01T08:23:42Z")

</div>

@PlatipuS

hello.

Could you please share the source code?

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [May 1, 2023, 12:06pm UTC](https://discourse.processing.org/t/issue-on-gps-accuracy-with-locationlistener/22332/6 "2023-05-01T12:06:56Z")

</div>

@GWAK

[https://android.processing.org/tutorials/location/index.html](https://android.processing.org/tutorials/location/index.html)

> <https://github.com/codeanticode/processing-android-tutorials/blob/master/location_permissions/gps_example/gps_example.pde>

---

<div class="post-metadata">

**Author:** ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)\
**Post date:** [May 1, 2023, 12:36pm UTC](https://discourse.processing.org/t/issue-on-gps-accuracy-with-locationlistener/22332/7 "2023-05-01T12:36:41Z")

</div>

@jafal

```auto
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", "initLocatiozn");

}

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

textAlign(LEFT, UP);

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_LOCAION") && hasLocation) {
textAlign(LEFT, UP);
text(locationListener.getCurrentLatitude() + "\n" +
locationListener.getCurrentLongitude() + "\n" +
"h: " + round((float)locationListener.getCurrentAltitude()) + " m" +
"P: " + round((float)locationListener.getCurrentAccuracy()) + " m"+
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, 500, 1, locationListener);
hasLocation = true;
} else {
hasLocation = false;
}
}

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;
}
}

```

‘No permissions to access location’

Only the text above is displayed in the app.

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [May 1, 2023, 1:09pm UTC](https://discourse.processing.org/t/issue-on-gps-accuracy-with-locationlistener/22332/8 "2023-05-01T13:09:28Z")

</div>

@GWAK hi

This works [Processing for Android](https://android.processing.org/tutorials/location/index.html) 1

 ![Screenshot_2023-05-01-16-04-48-643](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/1/d/1d7cff9612cfca991c0bf3f887de9e8b30f5bad3.jpeg)  
 ![Screenshot_2023-05-01-16-02-23-148](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/c/e/ce61d598c704e7eedb2a5aa9030aa9f1c9802d5f.jpeg)
