perform the function again via the same button

Asked

Viewed 31 times

0

Hello

i have a script for google markers that works like this:

i have a function that reads json, reads select, sees which or which options are selected in select within HTML, deletes markers (which are all) that are already marked and marks those that are selected.

all this after clicking on the filter button the problem that is giving is that if I change the selected options without refresh the page, does not execute the function and puts on the map all markers that are in json.

I wanted to know what I needed to put in the job so I could change the selected options and after clicking the button again executes the whole function again.

My filter button function script:

function botao() {
    var filtroFaixaEtaria = $("#FaixaEtaria option:selected").text();
    var filtroSexo = $("#Sexo option:selected").text();
    var filtroRecursoArma = $("#RecursoArma option:selected").text();
    var filtroDistrito = $("#Distrito option:selected").text();

    var arrayResultados = jsondata.filter(function (item) {
        var criteria = true;

        if (filtroFaixaEtaria != null && filtroFaixaEtaria != "")
            criteria = criteria && item["Faixa Etaria"] == filtroFaixaEtaria;
        if (filtroSexo != null && filtroSexo != "")
            criteria = criteria && item["Sexo"] == filtroSexo;
        if (filtroRecursoArma != null && filtroRecursoArma != "")
            criteria = criteria && item["RecursoArma"] == filtroRecursoArma;
        if (filtroDistrito != null && filtroDistrito != "")
            criteria = criteria && item["Distrito"] == filtroDistrito;

        return criteria;

    });

    for (var i = 0; i < markers.length; i++) {

        markers[i].setMap(null);
    }

    var iconBase = './imagens/iconmapa2.png';

    for (var i = 0; i < arrayResultados.length; i++) {
        var marker = new google.maps.Marker({
            position: markers[arrayResultados[i].id - 1].position,
            map: map,
            title: markers[arrayResultados[i].id - 1].title,
            id: markers[arrayResultados[i].id - 1].id,
            visible: true,
            icon: iconBase
        });

    }

    console.log("Foram encontrados " + arrayResultados.length + " elementos de acordo com a busca");

    document.getElementById("contadordebonecos").innerHTML = arrayResultados.length + " / " + jsondata.length;

    const imagens = Array.from({
        length: Number(arrayResultados.length)
    }).reduce((html) => html + imagem, '');
    $('#images').html(imagens);
}

$(document).on("click", "#btnBuscar", botao);

Basically just wanted to know what needs to be added in case you click the re-read the whole function again.

  • Your.log console is showing the correct amount or not?

  • @Andrévicente

  • I believe that you are creating 2 markers in the same place. To take out the Marker you use markers[i]. setMap(null); . Have you tried your for when it is time to show Marker again you place markers[arrayResults[i]. id - 1]. setMap(map); ? I could not test but I think that at the time it calls the new google.maps.Marker you create a new brand.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.