I'm having trouble deleting circle-like objects (API google-maps) being shown on my map.
I have a function that receives JSON data and creates a global array ( arrayElements ) containing objects ( element ) that have: JSON information and an < circle ) circle the google maps API. When I do a search (I get a new JSON), I access arrayElements , and for each element , I access the circle and run the function circle.setMap (null) , however, the problem is that some circles erase while others remain. I have debugged, but I could not identify the error.
var arrayElementos = [];
function setMapElements(mJsonData) {
if(arrayElementos.length==0){
var elementOptions = {
strokeColor: "#FF0000",
strokeOpacity: 0.8,
strokeWeight: 1,
fillColor: "#FF0000",
fillOpacity: 0.35,
map: map,
center: {},
radius: Math.sqrt(1) * 50
};
var elemento = {
idade:{},
genero:{},
circulo:{}
};
$.each(mJsonData, function(id,data){
elementOptions.center = new google.maps.LatLng(data.latitude,data.longitude);
elemento.genero = data.sexo;
elemento.idade = data.idade;
elemento.circulo = new google.maps.Circle(elementOptions);
mapElements.push(elemento);
});
}else{
$.each(arrayElementos, function(id,elemento){
elemento.circulo.setMap(null);
});
arrayElementos = [];
setMapElements(mJsonData);
}
};