我正在尝试从Google上的数组中添加一个自定义标记,我看到了另一个接近这个问题的问题,但是提供的代码不允许Google选择标记图标
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)]
});我一直得到一个“项目是没有定义的”。如果我将其定义在代码之上,则会在图标(itemsMath.floor(Math.random()*图标.length)代码上显示“无法读取未定义的属性'3‘”的不同错误。
如果有人知道解决方案,我将非常感激,谢谢!
发布于 2018-04-03 16:01:35
您正在尝试获取空变量items的内容..。替换
icon: items[Math.floor(Math.random()*icons .length)]通过以下方式:
icon: icons[Math.floor(Math.random()*icons .length)]因为您没有在变量items中保存图片,而是在变量icons中保存图片。
https://stackoverflow.com/questions/49634111
复制相似问题