Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reverse geocoding location runs asynchronously #301

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.Settings;
Expand Down Expand Up @@ -195,9 +196,9 @@ public void onClick(DialogInterface dialog, int which) {
private void setLocationFromPrefs() {
Log.d(TAG, "Setting location from preferences");
String longitude_s = PreferenceManager.getDefaultSharedPreferences(context)
.getString("longitude", "0");
.getString("longitude", "0");
String latitude_s = PreferenceManager.getDefaultSharedPreferences(context)
.getString("latitude", "0");
.getString("latitude", "0");

float longitude = 0, latitude = 0;
try {
Expand Down Expand Up @@ -252,33 +253,37 @@ public void onLocationChanged(Location location) {
Log.d(TAG, "LocationController -onLocationChanged");
}

private void showLocationToUser(LatLong location, String provider) {
private void showLocationToUser(final LatLong location, final String provider) {
// TODO(johntaylor): move this notification to a separate thread)
Log.d(TAG, "Reverse geocoding location");
Geocoder geoCoder = new Geocoder(context);
List<Address> addresses = new ArrayList<Address>();
String place = "Unknown";
try {
addresses = geoCoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
} catch (IOException e) {
Log.e(TAG, "Unable to reverse geocode location " + location);
}

if (addresses == null || addresses.size() == 0) {
Log.d(TAG, "No addresses returned");
place = String.format(context.getString(R.string.location_long_lat), location.getLongitude(),
location.getLatitude());
} else {
place = getSummaryOfPlace(location, addresses.get(0));
new AsyncTask<Void, Integer, String> (){
protected String doInBackground(Void... urls) {
Geocoder geoCoder = new Geocoder(context);
List<Address> addresses = new ArrayList<Address>();
String place = "Unknown";
try {
addresses = geoCoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
} catch (IOException e) {
Log.e(TAG, "Unable to reverse geocode location " + location);
}
if (addresses == null || addresses.size() == 0) {
Log.d(TAG, "No addresses returned");
place = String.format(context.getString(R.string.location_long_lat), location.getLongitude(),
location.getLatitude());
} else {
place = getSummaryOfPlace(location, addresses.get(0));
}
Log.d(TAG, "Location set to " + place);
return place;
}
protected void onPostExecute(String result) {
String messageTemplate = context.getString(R.string.location_set_auto);
String message = String.format(messageTemplate, provider, result);
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
}.execute();
}

Log.d(TAG, "Location set to " + place);

String messageTemplate = context.getString(R.string.location_set_auto);
String message = String.format(messageTemplate, provider, place);
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}

private String getSummaryOfPlace(LatLong location, Address address) {
String template = context.getString(R.string.location_long_lat);
String longLat = String.format(template, location.getLongitude(), location.getLatitude());
Expand Down