use of android.location.Address in project Remindy by abicelis.
the class FetchAddressIntentService method onHandleIntent.
@Override
protected void onHandleIntent(Intent intent) {
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
String errorMessage = "";
// Get the location passed to this service through an extra.
Location location = intent.getParcelableExtra(LOCATION_DATA_EXTRA);
mReceiver = intent.getParcelableExtra(RECEIVER);
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
} catch (IOException ioException) {
// Catch network or other I/O problems.
errorMessage = getString(R.string.service_not_available);
Log.e(TAG, errorMessage, ioException);
} catch (IllegalArgumentException illegalArgumentException) {
// Catch invalid latitude or longitude values.
errorMessage = getString(R.string.invalid_lat_long_used);
Log.e(TAG, errorMessage + ". " + "Latitude = " + location.getLatitude() + ", Longitude = " + location.getLongitude(), illegalArgumentException);
}
// Handle case where no address was found.
if (addresses == null || addresses.size() == 0) {
if (errorMessage.isEmpty()) {
errorMessage = getString(R.string.no_address_found);
Log.e(TAG, errorMessage);
}
deliverResultToReceiver(FAILURE_RESULT, errorMessage, "");
} else {
Address address = addresses.get(0);
ArrayList<String> addressFragments = new ArrayList<String>();
// join them, and send them to the thread.
for (int i = 0; i <= address.getMaxAddressLineIndex(); i++) {
addressFragments.add(address.getAddressLine(i));
}
Log.i(TAG, getString(R.string.address_found));
//deliverResultToReceiver(SUCCESS_RESULT, address.getFeatureName(), TextUtils.join(System.getProperty("line.separator"), addressFragments) );
deliverResultToReceiver(SUCCESS_RESULT, address.getFeatureName(), TextUtils.join(", ", addressFragments));
}
}
use of android.location.Address in project Ushahidi_Android by ushahidi.
the class GeocoderTask method doInBackground.
@Override
protected String doInBackground(Double... location) {
Log.i(getClass().getSimpleName(), String.format("doInBackground %s", Arrays.toString(location)));
Geocoder geoCoder = new Geocoder(context);
try {
List<Address> addresses = geoCoder.getFromLocation(location[0], location[1], 1);
StringBuilder addressString = new StringBuilder();
if (addresses != null && addresses.size() > 0) {
Address address = addresses.get(0);
if (address.getFeatureName() != null) {
addressString.append(address.getFeatureName());
}
if (address.getThoroughfare() != null) {
if (addressString.length() > 0) {
addressString.append(", ");
}
addressString.append(address.getThoroughfare());
}
if (address.getSubAdminArea() != null) {
if (addressString.length() > 0) {
addressString.append(", ");
}
addressString.append(address.getSubAdminArea());
}
if (address.getLocality() != null && !address.getLocality().equalsIgnoreCase(address.getSubAdminArea())) {
if (addressString.length() > 0) {
addressString.append(", ");
}
addressString.append(address.getLocality());
}
if (address.getCountryName() != null) {
if (addressString.length() > 0) {
addressString.append(", ");
}
addressString.append(address.getCountryName());
}
}
return addressString.toString();
} catch (IOException ioe) {
Log.e(getClass().getSimpleName(), "IOException", ioe);
} catch (IllegalArgumentException ioe) {
Log.e(getClass().getSimpleName(), "IllegalArgumentException", ioe);
}
return null;
}
use of android.location.Address in project HumaneApp by Ganesh1010.
the class GPSTracker method getAddressLine.
/**
* Try to get AddressLine
* @return null or addressLine
*/
public String getAddressLine(Context context) {
List<Address> addresses = getGeocoderAddress(context);
if (addresses != null && addresses.size() > 0) {
Address address = addresses.get(0);
String addressLine = address.getAddressLine(0);
return addressLine;
} else {
return null;
}
}
use of android.location.Address in project android_frameworks_base by crdroidandroid.
the class GeocoderTest method testGeocoder.
public void testGeocoder() throws Exception {
Locale locale = new Locale("en", "us");
Geocoder g = new Geocoder(mContext, locale);
List<Address> addresses1 = g.getFromLocation(37.435067, -122.166767, 2);
assertNotNull(addresses1);
assertEquals(1, addresses1.size());
Address addr = addresses1.get(0);
assertEquals("94305", addr.getFeatureName());
assertEquals("Palo Alto, CA 94305", addr.getAddressLine(0));
assertEquals("USA", addr.getAddressLine(1));
assertEquals("94305", addr.getPostalCode());
assertFalse(Math.abs(addr.getLatitude() - 37.4240385) > 0.1);
List<Address> addresses2 = g.getFromLocationName("San Francisco, CA", 1);
assertNotNull(addresses2);
assertEquals(1, addresses2.size());
addr = addresses2.get(0);
assertEquals("San Francisco", addr.getFeatureName());
assertEquals("San Francisco, CA", addr.getAddressLine(0));
assertEquals("United States", addr.getAddressLine(1));
assertEquals("San Francisco", addr.getLocality());
assertEquals("CA", addr.getAdminArea());
assertEquals(null, addr.getPostalCode());
assertFalse(Math.abs(addr.getLatitude() - 37.77916) > 0.1);
}
use of android.location.Address in project Android-ReactiveLocation by mcharmas.
the class GeocodeObservable method call.
@Override
public void call(Subscriber<? super List<Address>> subscriber) {
Geocoder geocoder = new Geocoder(ctx);
List<Address> result;
try {
if (bounds != null) {
result = geocoder.getFromLocationName(locationName, maxResults, bounds.southwest.latitude, bounds.southwest.longitude, bounds.northeast.latitude, bounds.northeast.longitude);
} else {
result = geocoder.getFromLocationName(locationName, maxResults);
}
if (!subscriber.isUnsubscribed()) {
subscriber.onNext(result);
subscriber.onCompleted();
}
} catch (IOException e) {
if (!subscriber.isUnsubscribed()) {
subscriber.onError(e);
}
}
}
Aggregations