我正在尝试从 Google map 上的数组添加自定义标记,我看到了另一个与此接近的问题,但提供的代码不允许 Google map 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
中保存图片。
关于javascript - 数组中的随机 Google map 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49634111/