我是JavaScript的初学者,现在我正在跟踪应用程序项目.现在我想随机更改Google地图标记颜色。
if(type == 'user')
{
var marker = new google.maps.Marker({
map: map,
icon: 'http://maps.google.com/mapfiles/ms/icons/yellow.png',
position: point,
label: icon.label
});
}
else
{
var marker = new google.maps.Marker({
map: map,
icon: 'http://maps.google.com/mapfiles/ms/icons/green.png',
position: point,
label: icon.label
});
// var pt = new google.maps.LatLng(point);
}
marker.addListener('click', function() {
infoWindow.setContent(infowincontent);
infoWindow.open(map, marker);
});
});
});
}
我在谷歌地图中得到很多相同颜色的用户,现在我想随机更改标记颜色.任何人请帮助我.
- 创建具有多种颜色的数组。
- 使用
Math.floor(Math.random() * SIZE_OF_ARRAY) + 1
生成值。 - 使用该随机值从值数组中动态选择颜色。
用你能拥有的所有 src 创建一个数组
var icons = ["http://maps.google.com/mapfiles/ms/icons/yellow.png",
"http://maps.google.com/mapfiles/ms/icons/red.png",
...etc
];
在icon:
你可以把items[Math.floor(Math.random()*icons .length)]