I am trying to get an address from latitude and longitude in Android Google Maps v2, but it errors.
Here is my code:
case PLACES_DETAILS :
final HashMap<String, String> hm = result.get(0);
final double latitude = Double.parseDouble(hm.get("lat"));
final double longitude = Double.parseDouble(hm.get("lng"));
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
googleMap = fm.getMap();
final LatLng point = new LatLng(latitude, longitude);
CameraUpdate cameraPosition = CameraUpdateFactory.newLatLngZoom(point, 10);
CameraUpdate cameraZoom = CameraUpdateFactory.zoomBy(5);
googleMap.moveCamera(cameraPosition);
googleMap.animateCamera(cameraZoom);
googleMap.getCameraPosition();
//Log.e("Lat", String.valueOf(googleMap.getCameraPosition().target.latitude));
//Log.e("Long", String.valueOf(googleMap.getCameraPosition().target.latitude));
//googleMap.setOnMyLocationChangeListener(myLocationChangeListener);
googleMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition cameraPosition) {
Log.e("Lat", String.valueOf(googleMap.getCameraPosition().target.latitude));
Log.e("Long", String.valueOf(googleMap.getCameraPosition().target.longitude));
}
});
Use the following code snippet:
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
}
reference : https://mobiforge.com/design-development/using-google-maps-android
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With