简体   繁体   English

如何更改点击按钮上的 map 标记颜色

[英]how to change map marker color on click button

in infowindow.setContent i have button i want to change marker color button on click i tried this codeinfowindow.setContent我有按钮我想更改标记颜色按钮点击我试过这个代码

      google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
          infowindow.setContent(locations[i][0] + locations[i][3] + " <button class='btn btn-primary btn-sm map-checked-box mr-1' onclick='myFunction();' data-id='"+ locations[i][3] +"'>Print</button> ");
          infowindow.open(map, marker);
        }
      })(marker, i ));
    }
    function myFunction() {
  infowindow.setContent('<div style="background-color: green">' + locations[i][0] + locations[i][3] + "</div>");
} 

i am calling myfunction() but it does not work in this line i think i am misiing locations in function我正在调用 myfunction() 但它在这一行中不起作用我想我在 function 中的错误位置

infowindow.setContent(locations[i][0] + locations[i][3] + " <button class='btn btn-primary btn-sm map-checked-box mr-1' onclick='myFunction();' data-id='"+ locations[i][3] +"'>Print</button> ");

You cannot change the color of a marker through CSS because markers are images.您不能通过 CSS 更改标记的颜色,因为标记是图像。 You will have to set a new image to the existing marker to get an effect of the changed marker color.您必须为现有标记设置新图像才能获得更改后的标记颜色的效果。

General steps would be:一般步骤是:

  1. Store the marker in a variable that is available later in myFunction .将标记存储在稍后在myFunction中可用的变量中。
  2. Update the marker image using setIcon(urlToNewImage)使用setIcon(urlToNewImage)更新标记图像

Here is an example fiddle .这是一个示例 fiddle Click on the change button to see the effect.单击更改按钮以查看效果。

Refer to this link for a list of icons available online.请参阅此链接以获取在线提供的图标列表。

resolved by解决了

function state(marker){ 
  marker.setIcon('http://maps.google.com/mapfiles/ms/micons/orange.png');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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