I have a map activity which also has an EditText on top of it and I want to show the changing address in that EditText whenever there's a drag on the map. I am currently using Geocoder class to retrieve address based on the coordinates provided by the map. But this way is causing a lot of lag in the fluent dragging motion of the map. Is there a better way to retrieve address or a more efficient way to use Geocoder class? Right now, I have implemented the getAddress() method inside the OnCameraChangeListener event of my map. Here is the code.
mMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition cameraPosition) {
double latitude=mMap.getCameraPosition().target.latitude;
double longitude=mMap.getCameraPosition().target.longitude;
String _Location="";
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
try {
List<Address> listAddresses = geocoder.getFromLocation(latitude, longitude, 1);
if(null!=listAddresses&&listAddresses.size()>0){
_Location = listAddresses.get(0).getAddressLine(0);
}
} catch (IOException e) {
e.printStackTrace();
}
EditText addressTxt=(EditText) findViewById(R.id.search_address_txt);
addressTxt.setText(_Location);
}
});
I have seen another app implemented where the address searched was at the end of the map drag and there was no lag in the map's motion.