0

我有问题,标签将保持黑色即使与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。 感谢任何能帮助我的人。

回答

7

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

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>

+0

谢谢@geocodezip对我来说非常完美:)。 – Johannes

0

尝试检查CSS中的“.labels”类,也许你应该改变CSS文件中的颜色。

+0

哦对不起我能有添加到我的文章,但CSS是好的,尽可能两种方式都没有worki ng对我来说:/ – Johannes