Javascript 不确定Google Maps的侦听器事件中值为何未定义
我试图从数组数组中获取字符串。我想在信息窗口中获取第一个属性[0]。我可以使用第二个和第三个值来设置标记的位置,还可以console.log第一个值 具体的JavaScript代码如下所示:Javascript 不确定Google Maps的侦听器事件中值为何未定义,javascript,arrays,google-maps,undefined,infowindow,Javascript,Arrays,Google Maps,Undefined,Infowindow,我试图从数组数组中获取字符串。我想在信息窗口中获取第一个属性[0]。我可以使用第二个和第三个值来设置标记的位置,还可以console.log第一个值 具体的JavaScript代码如下所示: markers = [ ["4841 Bethesda Avenue (Elm Street)", 38.980724, -77.0964], ["7171 Woodmont Avenue", 38.980097, -77.093662], ["921 Wayne Avenue", 38.995740, -
markers = [
["4841 Bethesda Avenue (Elm Street)", 38.980724, -77.0964],
["7171 Woodmont Avenue", 38.980097, -77.093662],
["921 Wayne Avenue", 38.995740, -77.025652],
["801 Ellsworth Drive",38.997778, -77.025071]
];
infoWindow = new google.maps.InfoWindow()
for (var i = 0; i < markers.length; i++) {
console.log("markers: " + markers[i][0]);
position = new google.maps.LatLng(markers[i][1], markers[i][2]);
marker = new google.maps.Marker({
position: position,
map: map,
title: markers[i][0]
});
console.log("markers 2: " + markers[i][0]);
google.maps.event.addListener(marker, 'click', function () {
infoWindow.setContent("<div>Hello, World" + markers[i][0] + "</div>");
infoWindow.open(map, this);
});
};
CSS:
JavaScript:
var map;
var mapProp;
var marker;
var markers;
var url;
var myData;
var time;
var available;
var total;
var facility;
var position;
var infoWindow;
function initialize() {
mapProp = {
center: new google.maps.LatLng(38.994890, -77.063416),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map"), mapProp);
}
$(document).ready(function() {
initialize();
url = 'https://data.montgomerycountymd.gov/resource/qahs-fevu.json';
$.getJSON(url, function(data) {
myData = data;
for (i = 0; i < myData.length; i++) {
time = (myData[i].asofdatetime).slice(11);
available = myData[i].space_count;
total = myData[i].total_spaces;
facility = myData[i].facilitynumber;
if (facility === "GAR 57") {
facility = "4841 Bethesda Avenue (Elm Street)";
$('#GAR57').html('As of ' + time + ' there are ' + available +
' of ' + total + ' at ' + facility);
} else if (facility === "GAR 31") {
facility = "7171 Woodmont Avenue";
$('#GAR31').html('As of ' + time + ' there are ' + available +
' of ' + total + ' at ' + facility);
} else if (facility === "GAR 60") {
facility = "921 Wayne Avenue";
$('#GAR60').html('As of ' + time + ' there are ' + available +
' of ' + total + ' at ' + facility);
} else {
facility = "801 Ellsworth Drive";
$('#GAR61').html('As of ' + time + ' there are ' + available +
' of ' + total + ' at ' + facility);
}
}
});
//set markers
markers = [
["4841 Bethesda Avenue (Elm Street)", 38.980724, -77.0964],
["7171 Woodmont Avenue", 38.980097, -77.093662],
["921 Wayne Avenue", 38.995740, -77.025652],
["801 Ellsworth Drive", 38.997778, -77.025071]
];
infoWindow = new google.maps.InfoWindow();
for (var i = 0; i < markers.length; i++) {
console.log("markers: " + markers[i][0]);
position = new google.maps.LatLng(markers[i][1], markers[i][2]);
marker = new google.maps.Marker({
position: position,
map: map,
title: markers[i][0]
});
console.log("markers 2: " + markers[i][0]);
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent("<div>Hello, World" + markers[i][0] + "</div>");
infoWindow.open(map, this);
});
};
});
var映射;
var-mapProp;
var标记;
var标记;
var-url;
var-myData;
var时间;
可用的var;
总风险价值;
风险价值工具;
var位置;
var信息窗口;
函数初始化(){
mapProp={
中心:新google.maps.LatLng(38.994890,-77.063416),
缩放:13,
mapTypeId:google.maps.mapTypeId.ROADMAP
};
map=新的google.maps.map(document.getElementById(“map”),mapProp);
}
$(文档).ready(函数(){
初始化();
url='1〕https://data.montgomerycountymd.gov/resource/qahs-fevu.json';
$.getJSON(url、函数(数据){
myData=数据;
对于(i=0;i
标记[i][0]变得未定义,因为它不存在于addListener中的函数范围内,即您没有将标记传递到函数中
相反,你是通过了标记
for(变量i=0;i
显示了单击标记时显示标题的示例。我假设这就是您想要实现的。使用@geocodezip中的代码函数闭包
(marker,I)
在函数末尾做了什么?这些是匿名函数的参数,该匿名函数返回click listener函数(`(function(marker,I){return function(){`
#map {
height: 300px;
width: 500px;
margin: 0 auto;
border: 1px solid black;
}
var map;
var mapProp;
var marker;
var markers;
var url;
var myData;
var time;
var available;
var total;
var facility;
var position;
var infoWindow;
function initialize() {
mapProp = {
center: new google.maps.LatLng(38.994890, -77.063416),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map"), mapProp);
}
$(document).ready(function() {
initialize();
url = 'https://data.montgomerycountymd.gov/resource/qahs-fevu.json';
$.getJSON(url, function(data) {
myData = data;
for (i = 0; i < myData.length; i++) {
time = (myData[i].asofdatetime).slice(11);
available = myData[i].space_count;
total = myData[i].total_spaces;
facility = myData[i].facilitynumber;
if (facility === "GAR 57") {
facility = "4841 Bethesda Avenue (Elm Street)";
$('#GAR57').html('As of ' + time + ' there are ' + available +
' of ' + total + ' at ' + facility);
} else if (facility === "GAR 31") {
facility = "7171 Woodmont Avenue";
$('#GAR31').html('As of ' + time + ' there are ' + available +
' of ' + total + ' at ' + facility);
} else if (facility === "GAR 60") {
facility = "921 Wayne Avenue";
$('#GAR60').html('As of ' + time + ' there are ' + available +
' of ' + total + ' at ' + facility);
} else {
facility = "801 Ellsworth Drive";
$('#GAR61').html('As of ' + time + ' there are ' + available +
' of ' + total + ' at ' + facility);
}
}
});
//set markers
markers = [
["4841 Bethesda Avenue (Elm Street)", 38.980724, -77.0964],
["7171 Woodmont Avenue", 38.980097, -77.093662],
["921 Wayne Avenue", 38.995740, -77.025652],
["801 Ellsworth Drive", 38.997778, -77.025071]
];
infoWindow = new google.maps.InfoWindow();
for (var i = 0; i < markers.length; i++) {
console.log("markers: " + markers[i][0]);
position = new google.maps.LatLng(markers[i][1], markers[i][2]);
marker = new google.maps.Marker({
position: position,
map: map,
title: markers[i][0]
});
console.log("markers 2: " + markers[i][0]);
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent("<div>Hello, World" + markers[i][0] + "</div>");
infoWindow.open(map, this);
});
};
});
for (var i = 0; i < markers.length; i++) {
console.log("markers: " + markers[i][0]);
position = new google.maps.LatLng(markers[i][1], markers[i][2]);
marker = new google.maps.Marker({
position: position,
map: map,
title: markers[i][0]
});
console.log("markers 2: " + marker.title);
google.maps.event.addListener(marker, 'click', function () {
infoWindow.setContent("<div>Hello, World " + marker.title + "</div>");
infoWindow.open(map, this);
});
};