Search code examples
androidgoogle-mapsmarker

Google map markers comparison


I add marker to a google map. And keep the reference as field of my class.

 MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(position)
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_aim))
                .anchor(0.5f, 0.5f)
                .draggable(false);
        mCurrentPositionMarker = mView.getGoogleMap().addMarker(markerOptions);

onMarkerClick(Marker marker) returns a reference. I added only one, so (marker == mCurrentPositionMarker) should be true, but it is false. Why?


Solution

  • From the docs (emphasis mine):

    The Maps API allows you to listen and respond to marker events. To listen to these events, you must set the corresponding listener on the GoogleMap object to which the markers belong. When the event occurs on one of the markers on the map, the listener's callback will be invoked with the corresponding Marker object passed through as a parameter. To compare this Marker object with your own reference to a Marker object, you must use equals() and not ==.