Home » Android » android – Update Custom Markers on Google Maps-Exceptionshub

android – Update Custom Markers on Google Maps-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

I’ve a problem with my custom marker on Google maps. I am displaying 50 markers at a time with a view pager at the bottom. I have custom markers populating the map with text, however when the user pages through the view pager, I then want to highlight each marker.

I keep the markers in a list and access that marker when the view pager page changes. When I try to call setIcon on the maker, it just shows the new icon over the old icon and doesn’t actually remove the old.

So adding the custom marker is as follows :

private fun addMarkerForAd(ad: DaftSearchAd, index: Int) {
        val location = LatLng(ad.point?.latitude!!, ad.point?.longitude!!)
        val icon = markerFactory.buildUnselectedIcon(ad.price)
        val options = MarkerOptions().position(location).icon(icon).anchor(0.5f, 1f).snippet(ad.price)
        propertyMarkers.append(index, googleMap.addMarker(options))
    }

Then for the selecting its:

    override fun selectMarker(propertyMarker: Marker) {
        propertyMarker.setIcon(null)
        propertyMarker.setIcon(markerFactory.buildSelectedIcon(propertyMarker.snippet))
}

There is also functionality to deselect the previously selected. When I do that on the previously selected:

    override fun deselectMarker() {
        previouslySelectedMarker?.setIcon(null)
        previouslySelectedMarker?.setIcon(markerFactory.buildUnselectedIcon(previouslySelectedMarker?.snippet))
    }

All that happens is the custom deselected icon is brought forward as I can see parts of the selected icon behind.

This has been driving me mad. I even tried to keep a list of the marker options, remove the marker from the map and add a new based on the updated marker options and that didn’t work. If anyone has any experience of this I’d appreciate it. I’ll scanned SO already and there doesn’t seem to be a definitive answer for my case

How to&Answers: