0
votes

I am displaying a Google map in an Android application with two markers. On the onMapReady method everything works well, the map renders and the markers appear with the relevant bitmaps and information.

I have even implemented a zoom boundary using the CameraUpdateFactory() function with LatLngBounds. Again, up to this point, everything works well and as expected. Here's the snippet:

override fun onMapReady(googleMap: GoogleMap?) {
  myGoogleMap = googleMap   

  // myMapMarker is a global variable of type MarkerOptions
  // the options are set only once here and never changed elsewhere in the code
  myMapMarker = MarkerOptions()
                .position(LatLng(34.1478, 118.1445))
                .title("My Location")
                .snippet("Description of my location...")
                .icon(BitmapDescriptorFactory.fromBitmap(pinBitmap))

  // myMapMarker is added to the map only once here and
  // never removed or added again elsewhere in the code
  myGoogleMap?.addMarker(myMapMarker)
}

However, when I try to reposition the marker using the marker setPosition function (just position in Kotlin), the marker position is changed but the pin remains in its original position, with the info-window still functional. I know that the marker position is updated because the next call to the zoom to bounds function zooms in to the new boundaries.

// only one attempt is made to move myMapMarker here 
// the marker location is successfully changed 
// >>> evidenced by println(myMapMarker?.location)
// the marker icon/pin is not re-positioned
myMapMarker?.position(LatLng(mapData.latitude, mapData.longitude))

I've read that the markers should be re-positioned and not removed & added again. So, is there a missing step in getting the marker to reposition correctly?

I've also tried to downgrade the Google Play Maps API from 17 to 16, but still getting the same results.

TIA.


EDIT: Adding a screenshot to illustrate the issue

enter image description here

The original marker was at 452 Ford Place (the red marker). After performing a setPosition(), the red marker should appear where the blue marker is, but it doesn't. It stays at 452 Ford Place.

1
what did you mean by pin?Abu Yousuf
The icon representing the marker that can be tapped to display the info window.iSofia
Can you provide any screen shot of the problemAbu Yousuf
@AbuYousuf I've expanded my answer with the screenshot. Any direction would be appreciated. Thank you.iSofia
I think your are reinitialize myMapMarker so that the previous marker stays sameAbu Yousuf

1 Answers

0
votes

The answer was staring me in the face the whole time.

I have been adding MarkerOptions to the map instead of Marker. That is why the position data (marker options) changed but not the marker position itself.

Problem solved!