use of android.location.Address in project Android-ReactiveLocation by mcharmas.
the class MainActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lastKnownLocationView = (TextView) findViewById(R.id.last_known_location_view);
updatableLocationView = (TextView) findViewById(R.id.updated_location_view);
addressLocationView = (TextView) findViewById(R.id.address_for_location_view);
currentActivityView = (TextView) findViewById(R.id.activity_recent_view);
locationProvider = new ReactiveLocationProvider(getApplicationContext());
lastKnownLocationObservable = locationProvider.getLastKnownLocation();
final LocationRequest locationRequest = LocationRequest.create().setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY).setNumUpdates(5).setInterval(100);
locationUpdatesObservable = locationProvider.checkLocationSettings(new LocationSettingsRequest.Builder().addLocationRequest(locationRequest).setAlwaysShow(//Refrence: http://stackoverflow.com/questions/29824408/google-play-services-locationservices-api-new-option-never
true).build()).doOnNext(new Action1<LocationSettingsResult>() {
@Override
public void call(LocationSettingsResult locationSettingsResult) {
Status status = locationSettingsResult.getStatus();
if (status.getStatusCode() == LocationSettingsStatusCodes.RESOLUTION_REQUIRED) {
try {
status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException th) {
Log.e("MainActivity", "Error opening settings activity.", th);
}
}
}
}).flatMap(new Func1<LocationSettingsResult, Observable<Location>>() {
@Override
public Observable<Location> call(LocationSettingsResult locationSettingsResult) {
return locationProvider.getUpdatedLocation(locationRequest);
}
});
addressObservable = locationProvider.getUpdatedLocation(locationRequest).flatMap(new Func1<Location, Observable<List<Address>>>() {
@Override
public Observable<List<Address>> call(Location location) {
return locationProvider.getReverseGeocodeObservable(location.getLatitude(), location.getLongitude(), 1);
}
}).map(new Func1<List<Address>, Address>() {
@Override
public Address call(List<Address> addresses) {
return addresses != null && !addresses.isEmpty() ? addresses.get(0) : null;
}
}).map(new AddressToStringFunc()).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
activityObservable = locationProvider.getDetectedActivity(50);
}
use of android.location.Address in project platform_frameworks_base by android.
the class LocationBasedCountryDetector method getCountryFromLocation.
/**
* @return the ISO 3166-1 two letters country code from the location
*/
protected String getCountryFromLocation(Location location) {
String country = null;
Geocoder geoCoder = new Geocoder(mContext);
try {
List<Address> addresses = geoCoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
if (addresses != null && addresses.size() > 0) {
country = addresses.get(0).getCountryCode();
}
} catch (IOException e) {
Slog.w(TAG, "Exception occurs when getting country from location");
}
return country;
}
use of android.location.Address in project android_frameworks_base by ParanoidAndroid.
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_frameworks_base by ParanoidAndroid.
the class LocationBasedCountryDetector method getCountryFromLocation.
/**
* @return the ISO 3166-1 two letters country code from the location
*/
protected String getCountryFromLocation(Location location) {
String country = null;
Geocoder geoCoder = new Geocoder(mContext);
try {
List<Address> addresses = geoCoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
if (addresses != null && addresses.size() > 0) {
country = addresses.get(0).getCountryCode();
}
} catch (IOException e) {
Slog.w(TAG, "Exception occurs when getting country from location");
}
return country;
}
use of android.location.Address in project XPrivacy by M66B.
the class ActivitySettings method search.
private void search() {
try {
String search = etSearch.getText().toString();
final List<Address> listAddress = new Geocoder(ActivitySettings.this).getFromLocationName(search, 1);
if (listAddress.size() > 0) {
Address address = listAddress.get(0);
// Get coordinates
if (address.hasLatitude()) {
cbLat.setChecked(false);
etLat.setText(Double.toString(address.getLatitude()));
}
if (address.hasLongitude()) {
cbLon.setChecked(false);
etLon.setText(Double.toString(address.getLongitude()));
}
// Get address
StringBuilder sb = new StringBuilder();
for (int i = 0; i <= address.getMaxAddressLineIndex(); i++) {
if (i != 0)
sb.append(", ");
sb.append(address.getAddressLine(i));
}
etSearch.setText(sb.toString());
}
} catch (Throwable ex) {
Toast.makeText(ActivitySettings.this, ex.getMessage(), Toast.LENGTH_LONG).show();
}
}
Aggregations