TypeError:google.maps.MarkerLabel不是构造函数

时间:2016-12-21 17:54:03

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

我想使用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不是一个costructor。好的,但是我应该把它叫做对象id(当然是一个数字)白色?

谢谢!

1 个答案:

答案 0 :(得分:6)

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

代码段

&#13;
&#13;
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);
&#13;
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
&#13;
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas"></div>
&#13;
&#13;
&#13;