简体   繁体   中英

How can I change the color of the markers (using leaflet), by click a button that is located in the infowindow of said marker?

The markers represented on my map come from my database. Each of those markers have a infowindow with two buttons, "Recusar" and "Aceitar", if the button "aceitar" is clicked I want to change the color of just the marker that I selected previously. If the button "recusar" is clicked I want that marker to be deleted of my map. The function called "aceite()" doesn't work.

<div id="map"></div>

 <script>
     var map = L.map('map').setView([39.694502, -8.130573], 7);
     L.tileLayer('https://api.maptiler.com/maps/topo/256/{z}/{x}/{y}.png?key=7YZ2VQZiOPItbtjSPYWe', {
    attribution: '<a href="https://www.maptiler.com/copyright/" target="_blank">&copy; MapTiler</a> <a href="https://www.openstreetmap.org/copyright" target="_blank">&copy; OpenStreetMap contributors</a>',
}).addTo(map);

$(document).ready(function () {
    $.ajax({
        type: 'GET',
        url: '/api/IgnicoesAPI',
        datatype: JSON,
        success: function (data) {
            $.each(data, function (i, item) {
                var infowindow = '<div id="content">' +
                    '<div id="siteNotice">' +
                    '</div>' +
                    '<div id="bodyContent">' +
                    '<p><b>Avaliação da Ocorrência:</b></p>' +
                    'Fotografias:' + '<div class="slider-holder">' +
                    '<span id="slider-image-1"></span>' +
                    '<span id="slider-image-2"></span>' +
                    '<span id="slider-image-3"></span>' +
                    '<div class="image-holder" >' +
                    '<img src="https://notalentforcertainty.files.wordpress.com/2015/09/woods-on-fire.jpg?w=1200" class="slider-image" style="width:60px; height:60px" />' +
                    '<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcTGlNA19yPsHNHW-n4mF3uhK7SnGkm029xfdQ1EhSyDPEmaGDM_" class="slider-image" style="width:60px; height:60px"/>' +
                    '<img src="https://wpde.com/resources/media/83f69602-210f-4ad1-bad0-bd5cf19470c7-large16x9_MARIONWOODSFIRE2.jpeg?1553519933510https://wpde.com/resources/media/83f69602-210f-4ad1-bad0-bd5cf19470c7-large16x9_MARIONWOODSFIRE2.jpeg?1553519933510" class="slider-image" style="width:60px; height:60px"/>' +
                    '</div>' +
                    '<div class="button-holder">' +
                    '<a href="#slider-image-1" class="slider-change"></a>' +
                    '<a href="#slider-image-2" class="slider-change"></a>' +
                    '<a href="#slider-image-3" class="slider-change"></a>' +
                    '</div>' +
                    '</div> ' +
                    '<p>Avaliação:' + item.estado + '</p>' +
                    '<p>Data:' + /*item.listaOcorrencias*/ + '</p></br>' +
                    '<button id="aceite" onclick="aceite()" >Aceitar</button>' +
                    '<button id="recusado">Recusar</button>' +
                    '<button id="concluido"> Concluído</button>' +
                    '</div>';
              var greenIcon = L.icon({
                  iconUrl: 'https://cdn3.iconfinder.com/data/icons/basicolor-signs-warnings/24/186_fire-512.png',
                  iconSize:     [35, 35], // size of the icon
shadowSize:   [50, 64], // size of the shadow
iconAnchor:   [22, 94], // point of the icon which will correspond to marker's location
shadowAnchor: [4, 62],  // the same for the shadow
popupAnchor:  [-3, -76] // point from which the popup should open relative to the iconAnchor


              });
                var marker = new L.marker([item.latitude, item.longitude], {icon: greenIcon})
                     .bindPopup(infowindow)
                    .on('click', onClick)
                    .addTo(map);

                $('#json map').append(marker);

            });
        },
        error: function (err) {
            console.error(err);
        }

    });
});




function onClick(e) {
    var popup = e.target.getPopup();
    var content = popup.getContent();

    console.log(content);
}


function aceite(e) {
    var greenIcon = L.icon({
        iconUrl: 'https://cdn3.iconfinder.com/data/icons/basicolor-signs-warnings/24/186_fire-512.png',
        iconSize: [35, 35], // size of the icon
        shadowSize: [50, 64], // size of the shadow
        iconAnchor: [22, 94], // point of the icon which will correspond to marker's location
        shadowAnchor: [4, 62],  // the same for the shadow
        popupAnchor: [-3, -76] // point from which the popup should open relative to the iconAnchor


    });

     marker = new L.marker({ icon: greenIcon })
        .addTo(map);
}

Can someone help me solve this problem?

Change your code to:

var clickedmarker = null;
function onClick(e) {
    var popup = e.target.getPopup();
    var content = popup.getContent();
    clickedmarker = e.target;

    console.log(content);
}


function aceite() {
    var greenIcon = L.icon({
        iconUrl: 'https://cdn3.iconfinder.com/data/icons/basicolor-signs-warnings/24/186_fire-512.png',
        iconSize: [35, 35], // size of the icon
        shadowSize: [50, 64], // size of the shadow
        iconAnchor: [22, 94], // point of the icon which will correspond to marker's location
        shadowAnchor: [4, 62],  // the same for the shadow
        popupAnchor: [-3, -76] // point from which the popup should open relative to the iconAnchor


    });

    if(clickedmarker){ //if the object exists and is not null / undefined
      clickedmarker.setIcon(greenIcon);
    }
    clickedmarker = null;
}

//For deleting the marker:
function recusado(){
    if(clickedmarker){ //if the object exists and is not null / undefined
      clickedmarker.removeFrom(map);
    }
    clickedmarker = null;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM