【问题标题】:Random Google Maps Marker from Array来自数组的随机谷歌地图标记
【发布时间】:2018-04-04 00:01:35
【问题描述】:

我正在尝试从 Google 地图上的数组中添加自定义标记我看到了另一个问题,但提供的代码不允许 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,让我们看看。
  • 嗨,我按照你的要求做了,现在我得到一个“未捕获的类型错误:无法读取未定义的属性 '5'”随机函数似乎工作,因为属性编号确实在不断变化,但没有标记已在地图上设置。
  • 为什么要声明空变量items
  • 我声明它是因为之前它说找不到,现在的问题是有一个错误说它无法读取该属性
  • 您正在尝试获取空变量 items 的内容... 将 icon: items[...............] 替换为 icon: icons[.......]

标签: javascript google-maps google-maps-api-3


【解决方案1】:

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

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

与:

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

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

【讨论】:

    猜你喜欢
    • 2013-11-18
    • 1970-01-01
    • 2011-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-14
    • 2014-09-26
    • 1970-01-01
    相关资源
    最近更新 更多