我有一个通过 JSON 提取的数据。它正在正确拉入并将标记定位在它们应该在的位置。我遇到的问题与google.maps.event.addListener
,所有标记都在信息窗口中显示相同的内容,由于某种原因,它似乎没有正确循环数据。
以下是我所拥有的,关于为什么它不能正常工作的任何想法,谢谢。
$.get('http://localhost/map/map/locations', function(result) {
var locations = $.parseJSON(result);
// Markers
var markers = [];
// Looping through the JSON data
for (var i = 0, length = locations.length; i < length; i++) {
var data = locations[i];
// Creating a marker and putting it on the map
var marker = new google.maps.Marker({
position: new google.maps.LatLng(data.lat, data.long),
map: map,
title: data.title,
icon: iconSrc[data.category]
});
markers.push(marker);
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infoWindow.setContent("<div class='cont_box' id='" + data.entry_id + "'> <h2>" + data.title + "</h2> <div class='cont'><p><img src='" + data.infoImage + "' alt='' width='100' height='66' style='float:right;'>" + data.windowText + "</p></div><div style='clear:both;'></div><div class='link'><a href='" + data.moreLink + "' target='_blank'>" + data.moreText + "</a></div></div>");
infoWindow.open(map, marker);
}
})(marker, i));
}// END for loop
});
更新
将数据变量添加到上下文后,它工作正常。
我现在必须根据它们的类别显示和隐藏标记。我认为这可能是一个类似的问题。将这些隐藏和显示功能包含在数据变量中的最佳方法是什么?
职能:
/** Shows all markers of a particular category, and ensures the checkbox is checked **/
function show(category) {
for (var i=0; i<locations.length; i++) {
if (data.category == category) {
markers[i].setVisible(true);
}
}
}// END function show
/** Hides all markers of a particular category, and ensures the checkbox is cleared **/
function hide(category) {
for (var i=0; i<locations.length; i++) {
if (data.category == category) {
markers[i].setVisible(false);
}
}
}// END function hide
/** Show or hide the categories initially **/
show("financial_services");
show("government");
show("identity_access");
show("machine_to_machine");
show("telecommunications");
show("transport");
/** Action when checkbox is clicked **/
$(".checkbox").click(function(){
var cat = $(this).attr("value");
// If checked
if ( $(this).is(":checked") ){
show(cat);
}
else {
hide(cat);
}
});
这就是它目前与所有东西一起使用的方式,它与 $.get 调用一起,但在其中包含数据 var 的 for 循环之外:
// JSON feed
$.get('http://localhost/map/map/locations', function(result) {
var locations = $.parseJSON(result);
// Markers
var markers = [];
// Looping through the JSON data
for (var i = 0, length = locations.length; i < length; i++) {
var data = locations[i];
// Creating a marker and putting it on the map
var marker = new google.maps.Marker({
position: new google.maps.LatLng(data.lat, data.long),
map: map,
title: data.title,
icon: iconSrc[data.category]
});
markers.push(marker);
google.maps.event.addListener(marker, 'click', (function(marker, i, loc) {
return function() {
infoWindow.setContent("<div class='cont_box' id='" + loc.entry_id + "'> <h2>" + loc.title + "</h2> <div class='cont'><p><img src='" + loc.infoImage + "' alt='' width='100' height='66' style='float:right;'>" + loc.windowText + "</p></div><div style='clear:both;'></div><div class='link'><a href='" + loc.moreLink + "' target='_blank'>" + loc.moreText + "</a></div></div>");
infoWindow.open(map, marker);
}
})(marker, i, data));
}// END for loop
/** Shows all markers of a particular category, and ensures the checkbox is checked **/
function show(category) {
for (var i=0; i<locations.length; i++) {
if (data.category == category) {
markers[i].setVisible(true);
}
}
}// END function show
/** Hides all markers of a particular category, and ensures the checkbox is cleared **/
function hide(category) {
for (var i=0; i<locations.length; i++) {
if (data.category == category) {
markers[i].setVisible(false);
}
}
}// END function hide
/** Show or hide the categories initially **/
show("financial_services");
show("government");
show("identity_access");
show("machine_to_machine");
show("telecommunications");
show("transport");
/** Action when checkbox is clicked **/
$(".checkbox").click(function(){
var cat = $(this).attr("value");
// If checked
if ( $(this).is(":checked") ){
show(cat);
}
else {
hide(cat);
}
});
});