Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 谷歌V3-参考标记_Javascript_Google Maps - Fatal编程技术网

Javascript 谷歌V3-参考标记

Javascript 谷歌V3-参考标记,javascript,google-maps,Javascript,Google Maps,我有一个脚本,可以在地图上逐个添加标记 var marker = new google.maps.Marker({ position: new google.maps.LatLng(51,-117) }); marker.setIcon(getIconFile(initialIconId)); markers.push(new Array(id,marker)); // id, marker marker.setMap(ma

我有一个脚本,可以在地图上逐个添加标记

var marker = new google.maps.Marker({
                    position: new google.maps.LatLng(51,-117)
                });
marker.setIcon(getIconFile(initialIconId));
markers.push(new Array(id,marker)); // id, marker
marker.setMap(map);
稍后在脚本中,当我按下自定义按钮时,我希望能够更改标记图标。因此,我从markers数组中按id获取marker并调用:

 markers[markerIndex].setIcon(getIconFile(newIconId)); // which returns a string url of the icon
但是我收到:TypeError:markers[markerId]。setIcon不是函数

我打印markerId,它是有效的,我还打印标记索引的结果[markerId],它返回一个标记对象。我没有别的办法调试这个我迷路了


谢谢

看起来您正在将一个数组推入标记中,而不仅仅是一个元素

为什么不:

markers = []
markers.push(marker)
markers[markerIndex].setIcon(getIconFile(newIconId));
或者,如果您坚持插入数组:

markers.push(new Array(id,marker));
markers[markerIndex][1].setIcon(getIconFile(newIconId));

因为我需要知道标记的id。每个标记代表一辆卡车,每个卡车都有一个唯一的id,使用索引没有帮助,因为它没有给我id。好的,我更新了我的答案…你可以使用
标记[markerIndex][1]
来获取标记