0

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 :)

Banana
  • 2,435
  • 7
  • 34
  • 60

2 Answers2

1

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

Community
  • 1
  • 1
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • I tried putting this code block in my onMapLongClick, but I get the following error for `Geocoder geocoder = new Geocoder(this, Locale.getDefault());` - geocoder (android.content.context, Locale) in geocoder cannot be applied to (anonymous com.google.android.gms.maps.android maps.onmaplongclicklistener) – Banana Jul 12 '16 at 09:28
  • 1
    @Kemo: Use Your current Activity Context instead of `this ` like if you are using Activity then use `YourActivityName. this` or if using Fragment then use `getActivity()` instead of `this` – ρяσѕρєя K Jul 12 '16 at 09:29
  • getActivity() worked. I also have another error - in `geocoder.getFromLocation(arg0.latitude, arg0.longitude, 1);` gets this error - unhandled exception java.ioexception, even if I tried: `try { ArrayList
    allAddresses = geocoder.getFromLocation(arg0.latitude, arg0.longitude, 1); } catch (IOException e) { e.printStackTrace(); }`
    – Banana Jul 12 '16 at 09:37
  • without try/catch I get unhandled exception java.ioexception, and with the try/catch I get incompatible types - required java.util.arrayList
    found java.util.array
    – Banana Jul 12 '16 at 09:42
  • @Kemo: Yes you need to wrap it inside `try/catch` block – ρяσѕρєя K Jul 12 '16 at 09:43
  • I did wrap it in try catch/block but I got _incompatible types - required java.util.arrayList
    found java.util.array
    _ error
    – Banana Jul 12 '16 at 09:44
  • 1
    @Kemo: Use `List ` instead of `ArrayList` – ρяσѕρєя K Jul 12 '16 at 09:46
1

You can always use the Geocoder:

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(arg0.latitude, arg0.longitude, 1);
String cityName = addresses.get(0).getAddressLine(0);
Akeshwar Jha
  • 4,516
  • 8
  • 52
  • 91