我想使用Google Maps V3自定义标记的标签颜色,然后在文档中搜索发现有一个名为google.maps.MarkerLabel的类。

在网上搜索时,我发现了一些用法的用法,这是我尝试做的事情:

        var marker = new google.maps.Marker ({
            position: new google.maps.LatLng(data.lat, data.lon),
            map: map,
            id: data.id,
            type: data.type,
            type_description: data.type_description,
            name: data.name,
            via: data.via,
            civico: data.civico,
            comune: data.comune,
            cap: data.cap,
            giorno: data.giorno,
            orario: data.orario,
            description: data.description,
            note: data.note,
            label: new google.maps.MarkerLabel({
                text: data.id,
                color: "white"
            }),
            icon: '<cms:link>../resources/images/' + data.markerIcon + '</cms:link>'
        });

该消息告诉我MarkerLabel不是构造函数。好的,但是我应该在哪里通过白色的对象ID(当然是数字)来调用它呢?

谢谢!

最佳答案

google.maps.MarkerLabel 没有构造函数,它是一个匿名对象。
像这样使用它:

var marker = new google.maps.Marker ({
        position: map.getCenter(),
        map: map,
        label: {
            text: "A", // data.id,
            color: "white"
        }
    });
proof of concept fiddle
代码段:

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: map.getCenter(), // new google.maps.LatLng(data.lat, data.lon),
    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?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="map_canvas"></div>

09-19 10:41