在for循环中使用多维数组设置google.maps.Marker图标
Set google.maps.Marker icon using multidimensional array in for loop
我一直在玩Google Maps Javascript API v3,并试图让它显示基于多维数组的图标。
一切都很好,直到我尝试从for循环中定义相对于"I"的图标,我认为我犯了一个简单的错误。
下面显示的代码给出错误:Uncaught TypeError: Cannot read property '2' of undefined
function initialize() {
var map;
var myOptions = {
zoom: 14,
center: new google.maps.LatLng(53.382971,-1.4702737),
mapTypeId: 'roadmap',
panControl: false,
streetViewControl: false,
mapTypeControl: false,
zoomControl: false
};
map = new google.maps.Map($('#map_canvas')[0], myOptions);
var addresses = [
["S1 4EJ", "Available", "/img/green-marker.png"],
["S1 4QW", "Available", "/img/green-marker.png"],
["S1 2HE", "Let Agreed", "/img/red-marker.png"]
];
for (var i = 0; i < addresses.length; i++) {
console.log(i);
$.getJSON('http://maps.googleapis.com/maps/api/geocode/json?address='+addresses[i][0]+'&sensor=false', null, function (data) {
var p = data.results[0].geometry.location
var latlng = new google.maps.LatLng(p.lat, p.lng);
console.log(i);
new google.maps.Marker({
position: latlng,
map: map,
icon: {
url: addresses[i][2],
size: new google.maps.Size(50, 50),
origin: new google.maps.Point(0,0),
anchor: new google.maps.Point(25, 50)
}
});
});
}
};
google.maps.event.addDomListener(window, "load", initialize);
$.getJSON
成功函数内的第二个console.log(addresses[i][2]);
全部输出为"3"。
将addresses[i][2]
移到$.getJSON
函数之外,但仍在for循环内部,将它们全部输出为"i"的最后一次调用迭代,例如:
...
for (var i = 0; i < addresses.length; i++) {
console.log(i);
var image = addresses[i][2];
$.getJSON('http://maps.googleapis.com/maps/api/geocode/json?address='+addresses[i][0]+'&sensor=false', null, function (data) {
var p = data.results[0].geometry.location
var latlng = new google.maps.LatLng(p.lat, p.lng);
console.log(i);
new google.maps.Marker({
position: latlng,
map: map,
icon: {
url: image,
size: new google.maps.Size(50, 50),
origin: new google.maps.Point(0,0),
anchor: new google.maps.Point(25, 50)
}
});
});
}
...
我是不是搞错了?
使用函数闭包保持循环中i
与创建的标记的关联。类似于这个问题:Google Maps JS API v3-Simple Multiple Marker Example为信息窗口内容做这件事。
for (var i = 0; i < addresses.length; i++) {
console.log(i);
var image = addresses[i][2];
$.getJSON('http://maps.googleapis.com/maps/api/geocode/json?address=' + addresses[i][0] + '&sensor=false', null, (function (i, image) {
return function (data) {
var p = data.results[0].geometry.location;
var latlng = new google.maps.LatLng(p.lat, p.lng);
console.log(i);
new google.maps.Marker({
position: latlng,
map: map,
icon: {
url: image,
size: new google.maps.Size(50, 50),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(25, 50)
}
});
}
})(i, image));
}
工作小提琴
代码片段:
function initialize() {
var map;
var myOptions = {
zoom: 14,
center: new google.maps.LatLng(53.382971, -1.4702737),
mapTypeId: 'roadmap',
panControl: false,
streetViewControl: false,
mapTypeControl: false,
zoomControl: false
};
map = new google.maps.Map($('#map_canvas')[0], myOptions);
var addresses = [
["S1 4EJ", "Available", "http://maps.google.com/mapfiles/ms/icons/green.png"],
["S1 4QW", "Available", "http://maps.google.com/mapfiles/ms/icons/green.png"],
["S1 2HE", "Let Agreed", "http://maps.google.com/mapfiles/ms/icons/blue.png"]
];
for (var i = 0; i < addresses.length; i++) {
console.log(i);
var image = addresses[i][2];
$.getJSON('http://maps.googleapis.com/maps/api/geocode/json?address=' + addresses[i][0] + '&sensor=false', null, (function (i, image) {
return function (data) {
var p = data.results[0].geometry.location;
var latlng = new google.maps.LatLng(p.lat, p.lng);
console.log(i);
new google.maps.Marker({
position: latlng,
map: map,
icon: {
url: image,
size: new google.maps.Size(50, 50),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(25, 50)
}
});
}
})(i, image));
}
}
google.maps.event.addDomListener(window, "load", initialize);
html, body, #map_canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas" style="border: 2px solid #3872ac;"></div>
相关文章:
- 如何在不使用jQuery的情况下设置数组以获取单选按钮值
- JavaScript-设置数组中包含变量的数组
- 用户输入设置数组长度
- jQuery/JavaScript按属性设置数组
- 在 JavaScript 中获取和设置数组内对象的值
- 无法设置数组中日期的格式
- 注册客户端脚本不适用于设置数组值
- 根据特定日期设置数组
- IMacros 检查和设置数组元素
- 通过索引设置数组对象的值会设置所有数组项
- 如何使用jQuery/javascript在下拉列表(多选)中设置数组对象的值
- MeanJS:在Angular中设置数组中的Mongoose对象引用
- 如何使用CSS在JavaScript中设置数组中“td”的高度
- 如何从新行设置数组的每个元素
- 根据单独数组中的值设置数组的值[Angular,forEach]
- 使用_.find设置数组内对象的属性值
- 以编程方式在聚合物中设置数组项的属性值
- Wp编辑器设置数组自动保存功能
- 如何设置数组的属性,以便通过json_encode获得所需的结果
- 如何在Javascript中设置数组中的JSON路径