Welcome back.
In this video, we're gonna allow the user to remove the markers from the screen.
So we created those markers.
When the user clicks on any marker, the selected marker custom info window will appear and change the
latitude and longitude.
Now we're gonna change this close button and make it as remove marker.
Remove marker.
And on click we need to set the selected marker to null.
Then we need to remove it from the selected markers.
If markers contains the position we need to markers dot remove.
Since we have two types of markers, one with single tap on marker on map and the other type is long
press.
So we have two types of markers blue and red.
The red is single click.
The blue is long click.
So we need to make this conditional check if markers dot contains position.
If the marker.
This marker is contained in single tapped markers, then go and remove it.
Otherwise, remove it from the long pressed markers.
So long clicked markers dot remove position.
Let me run.
Here we go.
Now let's create different markers.
Now let's click remove marker.
Click on this.
Click on this and the.
You notice that the selected and the custom info window is not appearing.
So like this.
Select this and we can remove markers.
Okay so there is a bug here.
We set the selected marker to null at the first.
And this is not not good.
We need to make it at the end.
Let me check create markers remove marker and the info window is not appearing.
I think there is an error in the logic.
So if I create a new markers and those are having the same, you notice that they are not changed.
This is being solved because I missed something very important.
So here we have two types.
The long clicked markers and the markers.
The single tap markers.
Okay.
Whenever you click on any marker of the single tap markers, we need to set here the onclick onClick
and specify the marker, the selected marker or my selected marker or single selected marker.
Selected marker equals to single.
Selected marker.
The position and return to to consume the event.
Similar to that we need to do in the long clicked markers.
Specify the onClick single selected marker, not single selected.
It's long, selected long and here long selected marker position.
Consume that event.
True.
Let me run again.
Add markers on the screen.
If I want to remove this or check this, you are updating the info card and remove the marker.
We remove this.
Click on this marker or this marker.
Everything is working fine.
Now we can remove any marker dynamically from the list.
Also let me test the long pressed once.
So here I need to create three markers.
Remove the middle and you can notice that everything is working fine.
So congratulations guys.
What we've done here.
We added the remove functionality to our application using this custom info window.
So let me make a quick recap what we've done here.
As I said before, we use the selected marker in order to create this custom info window.
And this selected marker will be used in order to initialize the position of the selected marker on
map click.
We added a selected marker equals to the my lat long, which is the parameter of the map.
Click lambda expression in the same way on map long click, we use this lat long to assign the selected
marker.
Then we add it inside the markers and the long clicked markers, which are the mutable state list of
lat long.
We added the onClick parameter.
We set the selected market to single selected marker to position, and the same way we added this to
the long selected marker.
Then we created those codes inside the button and we set this condition.
If the marker, the selected marker contains the position, the selected one the selected marker, then
go and remove from the markers and markers.
Define the single tapped markers.
Otherwise go and remove it from the long clicked markers.
Because we have two types of markers, the single tapped one and the long pressed markers.