use of android.location.Geocoder in project android_frameworks_base by DirtyUnicorns.
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.Geocoder in project YourAppIdea by Michenux.
the class AroundMeFragment method onCreate.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((YourApplication) getActivity().getApplication()).inject(this);
setRetainInstance(true);
setHasOptionsMenu(true);
mGoogleApiClient = new GoogleApiClient.Builder(this.getActivity()).addApi(LocationServices.API).addConnectionCallbacks(this).addOnConnectionFailedListener(this).build();
mRequest = LocationRequest.create().setInterval(15000).setFastestInterval(5000).setSmallestDisplacement(50).setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
mGeocoder = new Geocoder(this.getActivity(), Locale.getDefault());
mPlaceListAdapter = new PlaceRecyclerAdapter(new ArrayList<Place>());
if (savedInstanceState != null) {
this.mUseLocationClient = savedInstanceState.getBoolean("useLocationClient", true);
this.mCityName = savedInstanceState.getString("cityName");
this.mCurrentLocation = savedInstanceState.getParcelable("currentLocation");
this.mLocalProvider = savedInstanceState.getBoolean("localProvider", true);
}
if (this.mLocalProvider) {
mPlaceProvider = new PlaceLocalProvider(this, this);
} else {
mPlaceProvider = new PlaceRemoteProvider(this, this);
}
}
use of android.location.Geocoder in project android_frameworks_base by AOSPA.
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.Geocoder in project Space-Station-Tracker by Kiarasht.
the class Locations method Connected.
/**
* Lets find user's location.
*/
private void Connected() {
// Check if we have the right permissions
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, R.string.errorPermissionLocation, Toast.LENGTH_LONG).show();
return;
}
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
List<String> providers = locationManager.getProviders(true);
Location location = null;
for (int i = providers.size() - 1; i >= 0; i--) {
location = locationManager.getLastKnownLocation(providers.get(i));
if (location != null)
break;
}
// If we got something back start parsing
if (location != null) {
String url = "https://maps.googleapis.com/maps/api/staticmap?" + "center=LAT,LNG&" + "zoom=13&" + "scale=1&" + "size=640x640&" + "markers=color:red%7CLAT,LNG&" + "key=AIzaSyAtpWPhzhbtqTgofnQhAHjiG12MmrY2AAE";
mLatitude = String.valueOf(location.getLatitude());
mLongitude = String.valueOf(location.getLongitude());
url = url.replace("LAT", mLatitude);
url = url.replace("LNG", mLongitude);
try {
List<Address> matches = new Geocoder(this).getFromLocation(location.getLatitude(), location.getLongitude(), 1);
final Address bestMatch = (matches.isEmpty() ? null : matches.get(0));
if (bestMatch != null) {
String locationFormat = "";
if (!"null".equals(bestMatch.getLocality())) {
locationFormat += bestMatch.getLocality() + ", ";
}
if (!"null".equals(bestMatch.getAdminArea())) {
locationFormat += bestMatch.getAdminArea() + " ";
}
if (!"null".equals(bestMatch.getCountryCode())) {
locationFormat += bestMatch.getCountryCode() + " ";
}
if (!"null".equals(bestMatch.getPostalCode())) {
locationFormat += bestMatch.getPostalCode();
}
mTitleView.setText(locationFormat);
}
} catch (IOException e) {
e.printStackTrace();
}
Picasso.with(mActivity).load(url).into(mImageView);
displayResults();
displayPasses(null, null, null);
} else {
Toast.makeText(this, R.string.errorLocation, Toast.LENGTH_LONG).show();
}
}
use of android.location.Geocoder 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));
}
}
Aggregations