单击信息 window 内的按钮后更改 google 地图标记 属性

Changing google maps marker property after clicking button inside info window

我在地图上做了标记。我根据 JSON.

中的日期设置标记的不透明度

因此,当加载地图时,一些标记的不透明度为 0.5,一些为 1。

在标记的信息中 window 有一个按钮。单击此按钮时,我想将不透明度更改为 1。

下面是我的一些代码片段,向您展示我目前是如何设置它的。

任何帮助将不胜感激


//For every item in JSON
$.each(dbJSON, function(key, data) {

    var opacity = 1;
    var today = new Date();
    var fadeDate = new Date(data.last_rated); //get the date last rated                 
    fadeDate.setDate(fadeDate.getDate() + 1); //and add 1 date to it to specify the day when the icon should fade

    if(Date.parse(today) > Date.parse(fadeDate)) {
        console.log('fade');
        opacity = 0.5;
    } else {
        console.log('show');
        opacity = 1;
    }

    var postal_town = data.location;
    geocoder.geocode( { 'address': postal_town}, function(results, status) {
        //...
        console.log(opacity);

        var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location,
            title: data.manufacturer_name,
            icon: image,
            rating: data.rating,
            opacity: opacity
        });

        markers[data.id] = marker;

        marker.addListener('click', function() {

            var contentString = '<div id="content">'+
                '<h1>' + data.manufacturer_name + '</h1>' +
                '<button id="seen-it" data-rating="' + data.rating + '" data-entry-id="' + data.id + '">Seen it</button>' +
                '<p><strong>Rating: </strong><span id="rating">' + markers[data.id].rating + '</span></p>' +
                '</div>';

            infowindow.setContent(contentString);
            infowindow.open(map, marker);
        });
    });
});

//Do something when the #seen-it button is clicked
$(document).on('click', '#seen-it', function(event){
    //...
});

因为您将标记的 ID 存储在 data-entry-id 中,您可以使用(假设 markers 变量可从您的处理程序访问

$(document).on('click', '#seen-it', function(event){
    var markerId = $(this).data('entry-id');
    markers[markerId].setOpacity(1);
});