Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android google map - get map click location when marker clicked

I've got a map with plenty of large custom markers on it. Now I want to allow the user to create a path on the map (displayed as polyline and later saved as a list of geocoordinate pairs).

If the user clicks on the map I can collect these positions with the setOnMapClickedListener method of the map. But if the user clicks on a marker (setOnMarkerClickedListener), I can only retrieve the markers position (generally the position of the ancor of the marker).

Is it possible to somehow retrieve the clicked map location when actually a marker was clicked?

I'm using the SupportMapFragment.

like image 578
Oliver Metz Avatar asked Sep 19 '25 04:09

Oliver Metz


2 Answers

I think this is a bit late. But i hope this may help you.

mGoogleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {

           Toast.makeText(
                                YourActivity.this,
                                "Lat : " + latLng.latitude + " , "
                                        + "Long : " + latLng.longitude,
                                Toast.LENGTH_LONG).show();

            }
        });

        mGoogleMap.setOnInfoWindowClickListener(RegActivity.this);


    }
like image 59
Munem Sohan Avatar answered Sep 22 '25 19:09

Munem Sohan


Try like below

        mMap.setOnMarkerClickListener(new OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                LatLng position = marker.getPosition();

                Toast.makeText(
                                MainActivity.this,
                                "Lat " + position.latitude + " "
                                        + "Long " + position.longitude,
                                Toast.LENGTH_LONG).show();
                return true;
            }
        });
like image 20
srinu Avatar answered Sep 22 '25 19:09

srinu