Android : GPS Location on Button Press

on Friday, August 15, 2014


I am currently writing an app which will access the current location on the press of a button, and print it out. What I have right now is a method which gets the last known location. I have seen in many places that one is to implement a class that gets the location. My current code returns it as a string, but it only gets the location that was last updated by the phone. How can I change this so that when I press the button and call a method that uses this String, it will get the currentLocation rather than the lastLocation? Thank you so much! Also, This is my first ever android app, and I only have intermediate knowledge of Java and class implementation.



public String getLocation(){
String userLocation;

try {
LocationManager locMgr = (LocationManager) this.getSystemService(LOCATION_SERVICE);
Location recentLoc = locMgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);
String userLocator = recentLoc.toString();
userLocation = compressString(userLocator);
//userLocation = recentLoc.toString();

}
catch(Exception e) {
userLocation = "Location failed";
}

return userLocation;
}

0 comments:

Post a Comment