Get Map Address Or Location Address In Android
I am writing an app that requires to get the current map location. My Map file works fine by it self, but I need to get the address (see addressString below at the buttom) from ano
Solution 1:
I have used the following code in my app and its working absolutely fine for me. Try this. Hope this will help you.
//// Write the location name.//try {
Geocoder geo = new Geocoder(youractivityclassname.this.getApplicationContext(), Locale.getDefault());
List<Address> addresses = geo.getFromLocation(latitude, longitude, 1);
if (addresses.isEmpty()) {
yourtextfieldname.setText("Waiting for Location");
}
else {
if (addresses.size() > 0) {
yourtextfieldname.setText(addresses.get(0).getFeatureName() + ", " + addresses.get(0).getLocality() +", " + addresses.get(0).getAdminArea() + ", " + addresses.get(0).getCountryName());
//Toast.makeText(getApplicationContext(), "Address:- " + addresses.get(0).getFeatureName() + addresses.get(0).getAdminArea() + addresses.get(0).getLocality(), Toast.LENGTH_LONG).show();
}
}
}
catch (Exception e) {
e.printStackTrace(); // getFromLocation() may sometimes fail
}
Solution 2:
Either pass the address to your second Activity
in the Intent
, or if you need to do lookups from your second Activity
, just call Geocoder
from there.
If you're somehow instantiating the above class (GetMapActivity
) from your second Activity
and calling the getAddress
method, that won't work.
Solution 3:
The following might work [move the code from onCreate and put it in a method]
publicStringgetAddressString() {
if (addressString.equals("default") {
LocationManager lm = (LocationManager)act.getSystemService(Context.LOCATION_SERVICE);
Criteria crit = newCriteria();
crit.setAccuracy(Criteria.ACCURACY_FINE);
String provider = lm.getBestProvider(crit, true);
updateWithNewLocation(lm.getLastKnownLocation(provider));
}
return addressString;
}
Post a Comment for "Get Map Address Or Location Address In Android"