来自数组的随机谷歌地图标记

约什斯特

我正在尝试从 Google Maps 上的数组中添加自定义标记我看到了另一个与此接近的问题,但提供的代码不允许 Google Maps API 选择标记图标

 function button1(location) {
var get1 = prompt ("Enter First Coord");
var get2 = prompt ("Enter Second Coord");

var icons = [
"https://raw.githubusercontent.com/Concept211/Google-Maps-Markers/master/images/marker_green.png",
"https://raw.githubusercontent.com/Concept211/Google-Maps-Markers/master/images/marker_purple.png",
"https://raw.githubusercontent.com/Concept211/Google-Maps-Markers/master/images/marker_orange.png",
"https://raw.githubusercontent.com/Concept211/Google-Maps-Markers/master/images/marker_white.png",
"https://raw.githubusercontent.com/Concept211/Google-Maps-Markers/master/images/marker_black.png",
"https://raw.githubusercontent.com/Concept211/Google-Maps-Markers/master/images/marker_blue.png"

];
var items;
var center = new google.maps.LatLng(get1,get2);
map.panTo(center);
var marker = new google.maps.Marker({
position: center,
map: map,

icon: items[Math.floor(Math.random()*icons .length)]
});

我不断收到“项目未定义”。如果我在代码上方定义它,我会在图标上收到一个不同的错误,提示“无法读取未定义的属性 '3'”: items[Math.floor(Math.random()*icons .length)] 代码

如果有人知道解决方案,我将不胜感激,谢谢!

大卫·莫什科日

您正在尝试获取空变量的内容items...替换

icon: items[Math.floor(Math.random()*icons .length)]

和:

icon: icons[Math.floor(Math.random()*icons .length)]

因为你没有在变量items保存图片,而是在变量中icons

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章