Javascript 在google maps API中更改多个标记的颜色

Javascript 在google maps API中更改多个标记的颜色,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,我想问一下如何在谷歌地图中更改标记的颜色。条件是,我有一个在谷歌地图中创建多个标记的程序。但是我如何给每个标记指定颜色呢 这是我现在的代码 var标记=[]; var映射; 变量标签='ABCD'; var labelIndex=0; 功能添加标记(位置){ var marker=new google.maps.marker({ 位置:位置,, 标签:标签[labelIndex++%labels.length], 图标:'http://maps.google.com/mapfiles/ms/i

我想问一下如何在谷歌地图中更改标记的颜色。条件是,我有一个在谷歌地图中创建多个标记的程序。但是我如何给每个标记指定颜色呢

这是我现在的代码

var标记=[];
var映射;
变量标签='ABCD';
var labelIndex=0;
功能添加标记(位置){
var marker=new google.maps.marker({
位置:位置,,
标签:标签[labelIndex++%labels.length],
图标:'http://maps.google.com/mapfiles/ms/icons/green-dot.png'
地图:地图
});

markers.push(marker);
一种方法是将颜色传递到
addMarker
函数:

function addMarker(location, color) {
  var marker = new google.maps.Marker({
    position: location,
    label: labels[labelIndex++ % labels.length],
    icon: 'http://maps.google.com/mapfiles/ms/icons/'+color+'.png',
    map: map
  });
  markers.push(marker);
}

代码片段:

var标记=[];
var映射;
变量标签='ABCD';
var labelIndex=0;
函数初始化(){
map=新建google.maps.map(
document.getElementById(“地图画布”){
中心:新google.maps.LatLng(40.7127837,-74.0059413),
缩放:11,
mapTypeId:google.maps.mapTypeId.ROADMAP
});
//美国纽约州纽约市(40.7127837,-74.0059413)
//美国新泽西州纽瓦克(40.735657,-74.1723667)
//美国新泽西州泽西市(40.7281574999999,-74.07764170000002)
//美国新泽西州巴约内(40.6687141,-74.1143091000001)
添加标记({
拉脱维亚:40.7127837,
液化天然气:-74.0059413
}“红色”);
添加标记({
拉脱维亚:40.735657,
液化天然气:-74.1723667
}“绿色”);
添加标记({
纬度:40.7281575,
液化天然气:-74.0776417
}“黄色”);
添加标记({
纬度:40.6687141,
液化天然气:-74.1143091
}“橙色”);
}
google.maps.event.addDomListener(窗口“加载”,初始化);
功能添加标记(位置、颜色){
var marker=new google.maps.marker({
位置:位置,,
标签:标签[labelIndex++%labels.length],
图标:{
网址:'http://maps.google.com/mapfiles/ms/icons/“+color+”.png',
labelOrigin:新的google.maps.Point(15,10)
},
地图:地图
});
标记器。推(标记器);
}
html,
身体,
#地图画布{
身高:100%;
宽度:100%;
边际:0px;
填充:0px
}