Created
July 25, 2012 16:12
-
-
Save yasith/3177000 to your computer and use it in GitHub Desktop.
Android Button & Location
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Called when the user taps the Update Location Button | |
*/ | |
public void updateLocation(View view) { | |
Log.d(TAG, "Updating Location"); | |
TextView textView = (TextView) findViewById(R.id.location_label); | |
LocationManager locationManager = (LocationManager) | |
this.getSystemService(Context.LOCATION_SERVICE); | |
// Or use GPS_PROVIDER | |
String locationProvider = LocationManager.NETWORK_PROVIDER; | |
Location lastKnownLocation = | |
locationManager.getLastKnownLocation(locationProvider); | |
double latitude = lastKnownLocation.getLatitude(); | |
double longitude = lastKnownLocation.getLongitude(); | |
Geocoder gcd = new Geocoder(this, Locale.getDefault()); | |
List<Address> addresses = null; | |
String position = ""; | |
try { | |
addresses = gcd.getFromLocation(latitude, longitude, 1); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
if (addresses.size() > 0) { | |
position = addresses.get(0).getLocality(); | |
} | |
textView.setText(position); | |
} | |
/** | |
* Called when user taps the clear Location Button | |
*/ | |
public void clearLocation(View view) { | |
Log.d(TAG, "Clearing Location"); | |
TextView textView = (TextView) findViewById(R.id.location_label); | |
textView.setText(""); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment