首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >来自数组的随机Google地图标记

来自数组的随机Google地图标记
EN

Stack Overflow用户
提问于 2018-04-03 15:42:59
回答 1查看 242关注 0票数 0

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

代码语言:javascript
复制
 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‘”的不同错误。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-03 16:01:35

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

代码语言:javascript
复制
icon: items[Math.floor(Math.random()*icons .length)]

通过以下方式:

代码语言:javascript
复制
icon: icons[Math.floor(Math.random()*icons .length)]

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49634111

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档