Reputation: 11782
In android
I can easily get onLocationChanged
to work and gets the mobile latitude and longitude.
However once I got these coordinates How can I get the address of the mobile e.g
XYWZ Road, GRDSASDF Sector 823432, Australia etc
Upvotes: 0
Views: 2277
Reputation: 1697
Try this code:
public class LocationSpeecher extends MapActivity{
MapController mc;
MapView myMapView;
MapController mapController;
GeoPoint point;
MyPositionOverlay positionOverlay;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location_speecher);
MapView myMapView=(MapView)findViewById(R.id.myMapView);
mapController=myMapView.getController();
myMapView.displayZoomControls(true);
mapController.setZoom(17);
// myMapView.setSatellite(true);
myMapView.setStreetView(true);
myMapView.setTraffic(true);
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);
Criteria crta = new Criteria();
crta.setAccuracy(Criteria.ACCURACY_FINE);
crta.setAltitudeRequired(false);
crta.setBearingRequired(false);
crta.setCostAllowed(true);
crta.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(crta, true);
// String provider = LocationManager.GPS_PROVIDER;
Location location = locationManager.getLastKnownLocation(provider);
updateWithNewLocation(location);
locationManager.requestLocationUpdates(provider, 2000, 10, locationListener);
}
private final LocationListener locationListener = new LocationListener()
{
@Override
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
@Override
public void onProviderDisabled(String provider) {
updateWithNewLocation(null);
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
private void updateWithNewLocation(Location location) {
String latLong;
TextView myLocation;
myLocation = (TextView) findViewById(R.id.myLocation);
String addressString = "no address found";
if(location!=null) {
Double geoLat=location.getLatitude()*1E6;
Double geoLng=location.getLongitude()*1E6;
GeoPoint point=new GeoPoint(geoLat.intValue(),geoLng.intValue());
mapController.animateTo(point);
double lat = location.getLatitude();
double lon = location.getLongitude();
latLong = "Lat:" + lat + "\nLong:" + lon;
double lattitude = location.getLatitude();
double longitude = location.getLongitude();
Geocoder gc = new Geocoder(this,Locale.getDefault());
try {
List<Address> addresses= gc.getFromLocation(lattitude, longitude, 1);
StringBuilder sb = new StringBuilder();
if(addresses.size()>0) {
Address address=addresses.get(0);
for(int i=0;i<address.getMaxAddressLineIndex();i++)
sb.append(address.getAddressLine(i)).append("\n");
sb.append(address.getLocality()).append("\n");
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName());
}
addressString = sb.toString();
}
catch (Exception e) {
}
} else {
latLong = " NO Location Found ";
}
myLocation.setText("Current Position is :\n"+ latLong + "\n"+ addressString );
}
Upvotes: 0
Reputation: 31466
Geocoder is a class for handling Geocoding and Reverse Geocoding.
The amount of detail in a reverse geocoded location description may vary, for example one might contain the full street address of the closest building, while another might contain only a city name and postal code. The Geocoder class requires a backend service that is not included in the core android framework. The Geocoder query methods will return an empty list if there no backend service in the platform. Use the isPresent()
method to determine whether a Geocoder implementation exists.
The getFromLocation(double latitude, double longitude, int maxResults)
method returns an array of Addresses that are known to describe the area immediately surrounding the given latitude and longitude. The returned addresses will be localized for the locale provided to this class's constructor.
The returned values may be obtained by means of a network lookup. The results are a best guess and are not guaranteed to be meaningful or correct. It may be useful to call this method from a thread separate from your primary UI thread.
Here a Tutorial to get familiar with the Geocoder:
public class AndroidFromLocation extends Activity {
double LATITUDE = 37.42233;
double LONGITUDE = -122.083;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView myLatitude = (TextView)findViewById(R.id.mylatitude);
TextView myLongitude = (TextView)findViewById(R.id.mylongitude);
TextView myAddress = (TextView)findViewById(R.id.myaddress);
myLatitude.setText("Latitude: " + String.valueOf(LATITUDE));
myLongitude.setText("Longitude: " + String.valueOf(LONGITUDE));
Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);
try {
List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);
if(addresses != null) {
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder("Address:\n");
for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
}
myAddress.setText(strReturnedAddress.toString());
}
else{
myAddress.setText("No Address returned!");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
myAddress.setText("Canont get Address!");
}
}
}
Upvotes: 2
Reputation: 2461
First, you find out the lat long, which you said you have already done.
Then, you can use the following code to get the complete address in a list. Here is the code:
// this will fetch the data of current address
List<Address> addresses=geoCoder.getFromLocation(location.getLatitude(), location.getLongitude(), 10);
int i=1;
for(Address addObj:addresses)
{
// this will make the loop run for 1 time
if(i==1)
{
//variables to split address line
String add_line1_extract;
//setting street address
streetaddressText.setText(addObj.getAddressLine(0));
//splitting city and state
add_line1_extract=addObj.getAddressLine(1);
String string = add_line1_extract;
String[] parts = string.split(",");
//Setting city
part1 = parts[0];
cityText.setText(part1);
//setting state
String part2 = parts[1];
stateText.setText(part2);
//setting country
countryText.setText(addObj.getAddressLine(2));
i++;
progress.setVisibility(View.GONE);
}
}
Upvotes: 0
Reputation: 1611
Using GeoCoder Method.
Geocoder gcd = new Geocoder(context, Locale.getDefault());
List<Address> addresses = gcd.getFromLocation(lat, lng, 1);
if (addresses.size() > 0)
System.out.println(addresses.get(0).getLocality());
Upvotes: 0