The Google maps marker does not change the icon correctly when clicked

I am working with the API from google maps to javascript and I am loading markers that represent the position of deliverers(the Blue Motets) on the map, when clicking on a given deliverer I want his icon to change, to give the selection feedback to the user. See the image below.

when there is no dealer selected

To load the data for now I am doing a simulation via a list of objects (then this data will be puchados from a bench).

//classes de teste apagar depois 
        var Revenda = function (nomeFantasia, LatLng, icon) {
            this.nomeFantasia = nomeFantasia;
            this.LatLng = LatLng;
            this.icon = icon;
        }

        var Revendedor = function (nome, LatLng, Revenda, icon) {
            this.nome = nome;
            this.LatLng = LatLng;
            this.Revenda = Revenda;
            this.icon = icon;
        }

        var LatLng1 = {lat: -3.128518, lng: -59.965044}
        var LatLng2 = {lat: -3.120291, lng: -59.970366}
        var LatLng3 = {lat: -3.133789, lng: -59.980237}
        var LatLng4 = {lat: -3.125358, lng: -59.982636}
        var LatLng5 = {lat: -3.120291, lng: -59.981264}
        var LatLng6 = {lat: -3.127956, lng: -59.955013}

        var revenda1 = new Revenda("2 IRMÃOS", LatLng1, '../assets/mapa_revenda.png');
        var revenda2 = new Revenda("3 IRMÃOS", LatLng2, '../assets/mapa_revenda.png');
        var revenda3 = new Revenda("4 IRMÃOS", LatLng3, '../assets/mapa_revenda.png');

        revendasList = [
            revenda1,
            revenda2,
            revenda3
        ];

        var revendedor1 = new Revendedor("José", LatLng4, revenda1, '../assets/mapa_moto_sem_balao.png');
        var revendedor2 = new Revendedor("Maria", LatLng5, revenda2, '../assets/mapa_moto_sem_balao.png');
        var revendedor3 = new Revendedor("João", LatLng6, revenda3, '../assets/mapa_moto_sem_balao.png');

        revendedoresList = [
            revendedor1,
            revendedor2,
            revendedor3
        ];

As I will have a indeterminate amount of deliverers, I use a foreach to create the markers of the deliverers and inside the foreach I add the listeners click of the markers, and that's where my problem is, it only adds the icon exchange in the last instance traveled, this is bugging my implementation in any other deliverer it changes the icon of the last deliverer in the list, see the image below to understand better:

I clicked on the red icon

I clicked on the icon red and the icon of the right corner that was yellow , the code of the click of the markers is this:

 //Dynamic listing of markers
markersList = [];

revendedoresList.forEach(function (revendedorItemList) {
    // The marker, positioned at Uluru
    markerDelivery = new google.maps.Marker({
        position: revendedorItemList.LatLng, 
        map: map,
        icon: revendedorItemList.icon,
        tooltipData: "ENTREGADOR: " + revendedorItemList.nome +
        "<br>REVENDA: " + revendedorItemList.Revenda.nomeFantasia
    });

    markersList.push(markerDelivery);

    google.maps.event.addListener(markerDelivery, 'click', function (e) {
        modalFooterViewControl("none", "flex");
        markerDelivery.setIcon('../assets/mapa_moto_sem_balao_selecionado.png');

    });
});

I have already looked at the Google documentation and other questions in StackOverFlow (both in en-BR and in English) and found nothing that fits my need, if you can help I will be extremely grateful.

Author: Brendon Iwata, 2019-04-26

1 answers

After hitting head a few days and a lot of research in gringa's stackoverflow I was able to solve my problem, the problem was in the way I was using it setIcon, through the newly created marker instance. To be more precise in this specific code snippet:

google.maps.event.addListener(markerDelivery, 'click', function (e) {
    modalFooterViewControl("none", "flex");
    markerDelivery.setIcon('../assets/mapa_moto_sem_balao_selecionado.png');
});

To be correct one must call the seticon () method using the reserved word this, so my code was like this:

google.maps.event.addListener(markerDelivery, 'click', function (e) {
    modalFooterViewControl("none", "flex");
    this.setIcon('../assets/mapa_moto_sem_balao_selecionado.png');
});

Saw, simple ne. however it is it is worth noting that whenever you use setIcon, it resets the position of the Labels that are on your Marker, so be careful with this, but to change the map icon by clicking this will be enough. ;-)

 0
Author: Brendon Iwata, 2019-05-02 13:14:05