如何在点击按钮上更改地图标记的颜色

问题描述

infowindow.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>");
} 

我正在调用myfunction(),但在此行中不起作用。我认为我正在混淆函数中的位置

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> ");

解决方法

由于标记是图像,因此无法通过CSS更改标记的颜色。 您必须为现有标记设置一个新图像才能获得更改后的标记颜色的效果。

一般步骤为:

  1. 将标记存储在稍后在Expected: rec.get("recId") = first record rec.get("recId") = second record rec.get("recId") = third record* Actual: rec.get("recId") = third record rec.get("recId") = third record rec.get("recId") = third record 中可用的变量中。
  2. 使用myFunction
  3. 更新标记图像

Here is an example fiddle。单击更改按钮以查看效果。

Refer to this link获取在线可用图标的列表。

,

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