如何更改标记标签文字颜色?

时间:2016-10-14 11:51:57

标签: javascript google-maps google-maps-api-3 label google-maps-markers

我有一个问题,即使使用JavaScript中的labelColor属性,标签也会保持黑色,但我想让它变成白色是否有任何方法可以这样做?也许是一种解决方法?

for (i = 0; i < locations.length; i++) {var myLatLng = new google.maps.LatLng(locations[i][1], locations[i][2]);
marker = new google.maps.Marker({
    position: myLatLng,
    map: map,
    label: labels[i],
    labelClass: "labels", // the CSS class for the label
    labelColor: '#fff',
    labelInBackground: false,
    icon: locations[i][4]
});

问题是我有自己想要使用的自定义图标,我不想先创建一些SVG。 感谢任何可以帮助我的人。

2 个答案:

答案 0 :(得分:8)

要设置google.maps.Marker的标签属性,您需要使用MarkerLabel对象:

var marker = new google.maps.Marker({
  position: new google.maps.LatLng(37.4419, -122.1419),
  map: map,
  label: {
    text: 'A',
    color: 'white',
  }
});

代码段

var geocoder;
var map;

function initialize() {
  var map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(37.4419, -122.1419),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  var marker = new google.maps.Marker({
    position: new google.maps.LatLng(37.4419, -122.1419),
    map: map,
    label: {
      text: 'A',
      color: 'white',
    }
  });
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas"></div>

答案 1 :(得分:0)

尝试检查&#34; .labels&#34; CSS中的类,也许你应该在css文件中改变颜色。