p-lr / MapView

A Fast, memory efficient Android library to display tiled maps, with support for markers, paths, and rotation.
Apache License 2.0
184 stars 38 forks source link

MarkerView is not removed from the MapView #27

Closed sana-20 closed 2 years ago

sana-20 commented 2 years ago

Hi Peter.

I added a custom marker with an animation but it is not removed from the mapview.

class AnimMarker(context: Context) :  AppCompatImageView(context)

This method is called when the grey star button is clicked.

    private var animMarker : AnimMarker? = null

    private fun addAnimMarker(){
        animMarker = AnimMarker(requireContext()).apply{
            setImageResource(R.drawable.ic_twotone_lens_24)
            val anim = AlphaAnimation(0.0f, 1.0f)
            anim.duration = 1000
            anim.repeatCount = Animation.INFINITE
            anim.repeatMode = Animation.REVERSE
            this.startAnimation(anim)
        }

        mapView.addMarker(animMarker!!, 4096.0, 4096.0)
    }

This method is called when the yellow star button clicked again.

    private fun removeAnimMarker() {
        mapView.removeView(animMarker)
    }

Do you have any clue to solve this?

https://user-images.githubusercontent.com/66289794/129162353-f67fec1a-8d86-45b8-be34-45749908c34e.mp4

p-lr commented 2 years ago

Hi,

You should use MapView.removeMarker(marker) api, not removeView:

private fun removeAnimMarker() {
    mapView.removeMarker(animMarker)
}
sana-20 commented 2 years ago

Hi @peterLaurence.

It does not work though I changed the code as you answered. Not only does the marker still remain, but it is not anchored in the center position that I set. (4096, 4096) It moved whenever the map's zoom level or camera position changed.

https://user-images.githubusercontent.com/66289794/129294327-a08e0945-705b-49f7-9fc6-8df33b624c44.mp4

p-lr commented 2 years ago

Hi @sana-20 ,

I can't reproduce your issue. I've updated the demo of MapView to showcase how to remove a marker. Inside the "Map and Markers demo", you'll see a red circular marker which you can remove/add using the button at the bottom. See the code is in MapMarkersFragment of the demo.

I suggest that you make a github repo reproducing your issue.

sana-20 commented 2 years ago

Hi @peterLaurence,

I really appreciate your help. Adding and removing a marker works perfectly without an animation. But the tricky part is when I added an animation to a marker. Even though I clicked a remove button, it does not disappear. It looks like gone at glance, but when you changed the zoom level, you can see a marker still remaining.

See the code here.

p-lr commented 2 years ago

Indeed, with an animation set, I could reproduce. I managed to solve the issue by invoking clearAnimation() on the marker right before removing it. So for your use case:

private fun removeAnimMarker() {
    animMarker?.clearAnimation()
    mapView.removeMarker(animMarker)
}

You can use this fix in the meantime. I'll add clearAnimation in the MapView.removeMarker api.