2

I have a google map in my app and I handle clicks on it. Everything is fine but if I click on a marker or really close to it onMarkerClick() is called, not onMapClick() and then I don't have the exact location of the place I tapped because marker.getPosition() returns the center point of the marker what's not the same.

Is it possible to disable onMarkerClick() and have onMapClick() called even if I click on a marker?

Md. Sabbir Ahmed
  • 578
  • 5
  • 15
Giks91
  • 243
  • 1
  • 5
  • 14

2 Answers2

0

Return true for disabling marker click event

googleMap.setOnMarkerClickListener(new OnMarkerClickListener() {
    @Override
    public boolean onMarkerClick(Marker marker) {
        return true;
    }
});

For getting click on maps use this listener on onMapReady

@Override
public void onMapReady(GoogleMap googleMap) {
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener()
{
    @Override
    public void onMapClick(LatLng arg0)
    {
        Log.v("onMapClick", "Yea worked!");
    }
});
}

Complete code

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}


/**
 * Manipulates the map once available.
 * This callback is triggered when the map is ready to be used.
 * This is where we can add markers or lines, add listeners or move the camera. In this case,
 * we just add a marker near Sydney, Australia.
 * If Google Play services is not installed on the device, the user will be prompted to install
 * it inside the SupportMapFragment. This method will only be triggered once the user has
 * installed Google Play services and returned to the app.
 */
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney and move the camera
    LatLng sydney = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));

    googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
        @Override
        public void onMapClick(LatLng latLng) {
            Toast.makeText(MapsActivity.this, "Map clicked", Toast.LENGTH_SHORT).show();
        }
    });
    googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
        @Override
        public boolean onMarkerClick(Marker marker) {
            Toast.makeText(MapsActivity.this, "Marker disabled", Toast.LENGTH_SHORT).show();
            return true;
        }
    });
  }
}
Manoj Perumarath
  • 6,251
  • 4
  • 39
  • 57
  • Thanks but that's exactly what I did and the place where the marker is shown is not clicable but I expect onMapClick() to be called. – Giks91 Apr 04 '19 at 07:47
  • It's an open source project the class is here https://github.com/opendatakit/collect/blob/master/collect_app/src/main/java/org/odk/collect/android/map/GoogleMapFragment.java – Giks91 Apr 04 '19 at 07:55
  • Now there is no setOnMarkerClickListener there so the click is handled in a default way (a menu is shown on the bottom). If I add your solution nothing happens because it's consumed but still onMapClick() is not called then. So maybe it's not possible at all? – Giks91 Apr 04 '19 at 07:57
  • I did that and then the marker is not clickable indeed but still onMapClick() not called if I click on it. – Giks91 Apr 04 '19 at 08:09
  • can you try adding that marker click listener inside map click listener – Manoj Perumarath Apr 04 '19 at 08:12
  • I would appreciate! – Giks91 Apr 04 '19 at 08:25
  • I tried and after clicking on the marker or close to it I can see "Marker disabled" what is fine but still onMapClick() is not called. Did you experience something different? I want onMapClick() to be called even in that case. – Giks91 Apr 04 '19 at 08:55
-1

It is possible.Here is a simple solution.

First of all you should declare GoogleMap.OnMarkerClickListener and GoogleMap.OnMapClickListener in your activity or fragment like this.

public class MapActivity extends FragmentActivity implements 
GoogleMap.OnMarkerClickListener,
GoogleMap.OnMapClickListener
{
//..
}

And then register that interfaces in your onCreate() method.

@Override
protected void onCreate(Bundle savedInstanceState)
{   
    //..

    SupportMapFragment mapFragment = (SupportMapFragment)      getSupportFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    mMap.setOnMarkerClickListener(this);
    mMap.setOnMapClickListener(this);
}

Finally you can implement two interfaces.You should call onMapClick() method in onMarkerClick() method to achieve that even if you click on a marker like that.Don't forget to return true in onMarkerClick() method.

@Override
public void onMapClick(LatLng latLng) {
   Log.v("onMapClick", "Map Clicked..")
}

@Override
public boolean onMarkerClick(Marker marker) {
    onMapClick(marker.getPosition());
    return true;
}
  • 1
    But if you call onMapClick(marker.getPosition()); you use the center point of the marker what is not perfect since onMarkerClick() is called even if you click close to the marker, not exactly the center point. – Giks91 Apr 04 '19 at 08:52