getting location via GPS on android with Processing











up vote
0
down vote

favorite












im working on a small app that needs access to GPS so that i can track my position.
However i pasted some code that i can use to check if it works. I will rewrite it later so that it can be adjusted but for now i just want to try it.
BUT when i execute the app all paramaters stay the way they were initialized.
I do have all permissions enabled and also GPS enabled. Even went outside to check if it works, but it will always stay the same.
after the app asks if i allow the app to use gps service everything is executed correctly. It returns positive for location tracking.



Here is the code: (it can also be found under here: https://github.com/codeanticode/processing-android-tutorials/blob/master/location_permissions/ex1_gps/ex1_gps.pde)



/*****************************************************************************************
Android Processing GPS example

Query the phone's GPS and display the data on the screen

Rolf van Gelder - v 22/02/2011 - http://cage.nl :: http://cagewebdev.com :: info@cage.nl

Check the ACCESS_FINE_LOCATION permission in Sketch Permissions!

*****************************************************************************************/

// Import needed Android libs
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.provider.Settings;
import android.os.Bundle;
import android.Manifest;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;

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

// Variables to hold the current GPS data
float currentLatitude = 0;
float currentLongitude = 0;
float currentAccuracy = 0;
String currentProvider = "";

boolean hasLocation = false;

void setup () {
fullScreen();
orientation(PORTRAIT);
textFont(createFont("SansSerif", 26 * displayDensity));
textAlign(CENTER, CENTER);
requestPermission("android.permission.ACCESS_FINE_LOCATION", "initLocation");
}

void draw() {
background(0);
if (hasPermission("android.permission.ACCESS_FINE_LOCATION")) {
text("Latitude: " + currentLatitude + "n" +
"Longitude: " + currentLongitude + "n" +
"Accuracy: " + currentAccuracy + "n" +
"Provider: " + currentProvider, 0, 0, width, height);
} else {
text("No permissions to access location", 0, 0, width, height);
}
}

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 {
public void onLocationChanged(Location location) {
currentLatitude = (float)location.getLatitude();
currentLongitude = (float)location.getLongitude();
currentAccuracy = (float)location.getAccuracy();
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) {
}
}









share|improve this question






















  • Have you tried looking in the logs for errors?
    – Kevin Workman
    Nov 7 at 20:38










  • there are no errors. Everything runs smoothly except its not getting the data. It recognizes that gps permissions exist and gps access is allowed
    – Oliver Tworkowski
    Nov 7 at 21:44















up vote
0
down vote

favorite












im working on a small app that needs access to GPS so that i can track my position.
However i pasted some code that i can use to check if it works. I will rewrite it later so that it can be adjusted but for now i just want to try it.
BUT when i execute the app all paramaters stay the way they were initialized.
I do have all permissions enabled and also GPS enabled. Even went outside to check if it works, but it will always stay the same.
after the app asks if i allow the app to use gps service everything is executed correctly. It returns positive for location tracking.



Here is the code: (it can also be found under here: https://github.com/codeanticode/processing-android-tutorials/blob/master/location_permissions/ex1_gps/ex1_gps.pde)



/*****************************************************************************************
Android Processing GPS example

Query the phone's GPS and display the data on the screen

Rolf van Gelder - v 22/02/2011 - http://cage.nl :: http://cagewebdev.com :: info@cage.nl

Check the ACCESS_FINE_LOCATION permission in Sketch Permissions!

*****************************************************************************************/

// Import needed Android libs
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.provider.Settings;
import android.os.Bundle;
import android.Manifest;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;

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

// Variables to hold the current GPS data
float currentLatitude = 0;
float currentLongitude = 0;
float currentAccuracy = 0;
String currentProvider = "";

boolean hasLocation = false;

void setup () {
fullScreen();
orientation(PORTRAIT);
textFont(createFont("SansSerif", 26 * displayDensity));
textAlign(CENTER, CENTER);
requestPermission("android.permission.ACCESS_FINE_LOCATION", "initLocation");
}

void draw() {
background(0);
if (hasPermission("android.permission.ACCESS_FINE_LOCATION")) {
text("Latitude: " + currentLatitude + "n" +
"Longitude: " + currentLongitude + "n" +
"Accuracy: " + currentAccuracy + "n" +
"Provider: " + currentProvider, 0, 0, width, height);
} else {
text("No permissions to access location", 0, 0, width, height);
}
}

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 {
public void onLocationChanged(Location location) {
currentLatitude = (float)location.getLatitude();
currentLongitude = (float)location.getLongitude();
currentAccuracy = (float)location.getAccuracy();
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) {
}
}









share|improve this question






















  • Have you tried looking in the logs for errors?
    – Kevin Workman
    Nov 7 at 20:38










  • there are no errors. Everything runs smoothly except its not getting the data. It recognizes that gps permissions exist and gps access is allowed
    – Oliver Tworkowski
    Nov 7 at 21:44













up vote
0
down vote

favorite









up vote
0
down vote

favorite











im working on a small app that needs access to GPS so that i can track my position.
However i pasted some code that i can use to check if it works. I will rewrite it later so that it can be adjusted but for now i just want to try it.
BUT when i execute the app all paramaters stay the way they were initialized.
I do have all permissions enabled and also GPS enabled. Even went outside to check if it works, but it will always stay the same.
after the app asks if i allow the app to use gps service everything is executed correctly. It returns positive for location tracking.



Here is the code: (it can also be found under here: https://github.com/codeanticode/processing-android-tutorials/blob/master/location_permissions/ex1_gps/ex1_gps.pde)



/*****************************************************************************************
Android Processing GPS example

Query the phone's GPS and display the data on the screen

Rolf van Gelder - v 22/02/2011 - http://cage.nl :: http://cagewebdev.com :: info@cage.nl

Check the ACCESS_FINE_LOCATION permission in Sketch Permissions!

*****************************************************************************************/

// Import needed Android libs
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.provider.Settings;
import android.os.Bundle;
import android.Manifest;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;

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

// Variables to hold the current GPS data
float currentLatitude = 0;
float currentLongitude = 0;
float currentAccuracy = 0;
String currentProvider = "";

boolean hasLocation = false;

void setup () {
fullScreen();
orientation(PORTRAIT);
textFont(createFont("SansSerif", 26 * displayDensity));
textAlign(CENTER, CENTER);
requestPermission("android.permission.ACCESS_FINE_LOCATION", "initLocation");
}

void draw() {
background(0);
if (hasPermission("android.permission.ACCESS_FINE_LOCATION")) {
text("Latitude: " + currentLatitude + "n" +
"Longitude: " + currentLongitude + "n" +
"Accuracy: " + currentAccuracy + "n" +
"Provider: " + currentProvider, 0, 0, width, height);
} else {
text("No permissions to access location", 0, 0, width, height);
}
}

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 {
public void onLocationChanged(Location location) {
currentLatitude = (float)location.getLatitude();
currentLongitude = (float)location.getLongitude();
currentAccuracy = (float)location.getAccuracy();
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) {
}
}









share|improve this question













im working on a small app that needs access to GPS so that i can track my position.
However i pasted some code that i can use to check if it works. I will rewrite it later so that it can be adjusted but for now i just want to try it.
BUT when i execute the app all paramaters stay the way they were initialized.
I do have all permissions enabled and also GPS enabled. Even went outside to check if it works, but it will always stay the same.
after the app asks if i allow the app to use gps service everything is executed correctly. It returns positive for location tracking.



Here is the code: (it can also be found under here: https://github.com/codeanticode/processing-android-tutorials/blob/master/location_permissions/ex1_gps/ex1_gps.pde)



/*****************************************************************************************
Android Processing GPS example

Query the phone's GPS and display the data on the screen

Rolf van Gelder - v 22/02/2011 - http://cage.nl :: http://cagewebdev.com :: info@cage.nl

Check the ACCESS_FINE_LOCATION permission in Sketch Permissions!

*****************************************************************************************/

// Import needed Android libs
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.provider.Settings;
import android.os.Bundle;
import android.Manifest;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;

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

// Variables to hold the current GPS data
float currentLatitude = 0;
float currentLongitude = 0;
float currentAccuracy = 0;
String currentProvider = "";

boolean hasLocation = false;

void setup () {
fullScreen();
orientation(PORTRAIT);
textFont(createFont("SansSerif", 26 * displayDensity));
textAlign(CENTER, CENTER);
requestPermission("android.permission.ACCESS_FINE_LOCATION", "initLocation");
}

void draw() {
background(0);
if (hasPermission("android.permission.ACCESS_FINE_LOCATION")) {
text("Latitude: " + currentLatitude + "n" +
"Longitude: " + currentLongitude + "n" +
"Accuracy: " + currentAccuracy + "n" +
"Provider: " + currentProvider, 0, 0, width, height);
} else {
text("No permissions to access location", 0, 0, width, height);
}
}

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 {
public void onLocationChanged(Location location) {
currentLatitude = (float)location.getLatitude();
currentLongitude = (float)location.getLongitude();
currentAccuracy = (float)location.getAccuracy();
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) {
}
}






java permissions location processing android-gps






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 7 at 20:33









Oliver Tworkowski

204




204












  • Have you tried looking in the logs for errors?
    – Kevin Workman
    Nov 7 at 20:38










  • there are no errors. Everything runs smoothly except its not getting the data. It recognizes that gps permissions exist and gps access is allowed
    – Oliver Tworkowski
    Nov 7 at 21:44


















  • Have you tried looking in the logs for errors?
    – Kevin Workman
    Nov 7 at 20:38










  • there are no errors. Everything runs smoothly except its not getting the data. It recognizes that gps permissions exist and gps access is allowed
    – Oliver Tworkowski
    Nov 7 at 21:44
















Have you tried looking in the logs for errors?
– Kevin Workman
Nov 7 at 20:38




Have you tried looking in the logs for errors?
– Kevin Workman
Nov 7 at 20:38












there are no errors. Everything runs smoothly except its not getting the data. It recognizes that gps permissions exist and gps access is allowed
– Oliver Tworkowski
Nov 7 at 21:44




there are no errors. Everything runs smoothly except its not getting the data. It recognizes that gps permissions exist and gps access is allowed
– Oliver Tworkowski
Nov 7 at 21:44












1 Answer
1






active

oldest

votes

















up vote
0
down vote













The solution:




  • change the "0, 0" in locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); to a "1000, 0" depending on how often you want to get your current position in this case: 1000 ms. Im not gonna talk about the second 0.


  • also instead of using NETWORK_PROVIDER use GPS_PROVIDER to get your true GPS pos instead of networked pos.







share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53197389%2fgetting-location-via-gps-on-android-with-processing%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    The solution:




    • change the "0, 0" in locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); to a "1000, 0" depending on how often you want to get your current position in this case: 1000 ms. Im not gonna talk about the second 0.


    • also instead of using NETWORK_PROVIDER use GPS_PROVIDER to get your true GPS pos instead of networked pos.







    share|improve this answer

























      up vote
      0
      down vote













      The solution:




      • change the "0, 0" in locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); to a "1000, 0" depending on how often you want to get your current position in this case: 1000 ms. Im not gonna talk about the second 0.


      • also instead of using NETWORK_PROVIDER use GPS_PROVIDER to get your true GPS pos instead of networked pos.







      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        The solution:




        • change the "0, 0" in locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); to a "1000, 0" depending on how often you want to get your current position in this case: 1000 ms. Im not gonna talk about the second 0.


        • also instead of using NETWORK_PROVIDER use GPS_PROVIDER to get your true GPS pos instead of networked pos.







        share|improve this answer












        The solution:




        • change the "0, 0" in locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); to a "1000, 0" depending on how often you want to get your current position in this case: 1000 ms. Im not gonna talk about the second 0.


        • also instead of using NETWORK_PROVIDER use GPS_PROVIDER to get your true GPS pos instead of networked pos.








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 at 13:39









        Oliver Tworkowski

        204




        204






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53197389%2fgetting-location-via-gps-on-android-with-processing%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Academy of Television Arts & Sciences

            L'Équipe

            1995 France bombings