I was having some problem trying to filter the list fetched from database upon checkbox action and plot/clear markers onto map. Here is my checkbox declaration in HTML:
<div class="">
<label>
<input type="checkbox" class="js-switch" name="tags" value="3" onchange="filterList()" unchecked/> MRT Incidents
</label>
</div>
When checkbox onchange, I am filtering the list fetched from database:
function filterList(){
var tags = document.getElementsByName('tags');
var i = 0;
{% for crisis in data %}
// code to store fields in database into local var
for( ; i < tags.length; i++ )
{
if( tags[i].checked ) {
value = tags[i].value;
// I got 4 category 1-fire, 2-flood, 3-mrt, 4-dengue
// what I am doing here is check if the checkbox value is equal to the category ID from database, if equal, I push them into the filteredList
if(value == category){
filteredList.push({
// all the fields
});
} break;
}else{
value = tags[i].value;
if(value == category){
filteredList = [];
setMapOnAll(null);
}
}
}
{% endfor %}
addMarker(filteredList);
}
Then in my addMarker with filteredList parameter:
function addMarker(filteredList){
for(var i = 0; i < filteredList.length; i++){
myLatLng = {lat: parseFloat(filteredList[i].lat), lng: parseFloat(filteredList[i].lng) };
marker = new google.maps.Marker({
position: myLatLng,
map: map,
clickable: true
});
}
My plotting works weird as well. When I try to check multiple box, let's say I checked the first one, it plotted out. Then I proceed to check the second, it does not plot out but only plot out after I uncheck the first one.
When I try to uncheck the checkbox, the markers on the map are not removed. Instead, it just stay there forever and stacked more and more when I check/uncheck a few times.
Why is it so? Thanks in advance!
From what I can see in your code, you are adding the markers over and over again? This probably explains why you are having weird plotting (and you will run out of memory sooner or later). I think you are using the wrong approach though for marker filtering on a Google Map.
You'll need to make changes to your code, here are the main guidelines :
[{markerObj:marker, categories:[1,5,6]}]
markerObj.setVisible(false)
Here's a working example! https://jsfiddle.net/fjqagp8o/1/
You need to remove the markers before adding new ones.
I worked on Android's Google Map API, where I didn't have a method to access existing markers, so I'm not sure whether this is also correct for javascript.
So what I did is put them when adding them in a HashMap and removed them one by one.
That's the code for android:
private void clearAllPins() {
for (Marker marker : markersMap.values()) { //in your case a simple array might sufice
marker.remove();
}
clientsMarker.clear();
}
For remove (hide) a marker you must use
marker.setMap(null);
Your problem seems related to that fact that you don't have a collection of marker you have added ..then you can't set to null map for this markes If you have this collection you can hide all and then add the new ones