Kemo Kemo - 4 months ago 12
Java Question

Display a city on long click in Android

I am passing data between two activities. I want to do this: when the user long clicks on the map the new InfoActivity window opens, displaying latitude, longitude and the city where the longclick was made.

I already passed lat and long, but got stuck with the city part.

@Override
public void onMapLongClick(LatLng arg0) {

Intent intent = new Intent(getActivity(), InfoActivity.class);
intent.putExtra("latitude", arg0.latitude);
intent.putExtra("longitude", arg0.longitude);
startActivity(intent);

}


How would I get the city where the map is long clicked and add pass it to my InfoActivity.

Any help is appreciated :)

Answer

How would I get the city where the map is long clicked

Use Geocoder class with arg0 for getting city where the map is long clicked.like:

 Geocoder geocoder = new Geocoder(this, Locale.getDefault());
 List<Address> allAddresses = geocoder.getFromLocation(
                               arg0.latitude, arg0.longitude, 1);

Now get all details from allAddresses which is required for long clicked point.

and add pass it to my InfoActivity

For passing String between two Activities see following post:

Passing strings between activities in android