我有一个创建谷歌地图标记的函数。我从数据库查询中传入数据。然后我让函数使用一个值作为外键执行另一个数据库查询。然后我试图将所有结果(来自第一个查询的数据和来自第二个查询的数据)放入一个字符串中,标记将显示在 infoWindow 中。
但不知何故,程序将字符串视为“未定义”,除非我在 $.post 函数之外为第二个查询构建它。这是怎么回事?程序不应该能够读取该字符串吗?
这是我的代码:
function createMarker(marker_id, point,street, neighborhood, date,map) {
// Create the HTML text based on the values passed in from XML
$.post('get_victimdata.php', {marker_id:marker_id},
function(victimdata){
objVictimdata = jQuery.parseJSON(victimdata);
markerhtml = "";
markerhtml += "<strong>Street: </strong>" + street + "<br>";
markerhtml += "<strong>Neighborhood: </strong>" + neighborhood + "<br>";
markerhtml += "<strong>Date: </strong>" + date + "<br><br>";
for (var i=0; i < objVictimdata.length; i++) {
var image_path = objVictimdata[i].image_path;
var image_height = objVictimdata[i].image_height;
markerhtml += "<strong>Victim Name: </strong>" + objVictimdata[i].name + "<br>";
markerhtml += "<strong>Age:</strong> " + objVictimdata[i].age + "<br>";
markerhtml += "<strong>Race:</strong> " + objVictimdata[i].race + "<br>";
markerhtml += "<strong>Gender:</strong> " + objVictimdata[i].gender + "<br>";
markerhtml += "<strong>Cause:</strong> " + objVictimdata[i].cause + "<br><br>";
console.log(markerhtml);
}//end for
})//end post
console.log(markerhtml);
var image = new google.maps.MarkerImage('http://labs.google.com/ridefinder/images/mm_20_green.png',
new google.maps.Size(12, 20),
new google.maps.Point(0,0),
new google.maps.Point(6, 20));
var marker = new google.maps.Marker({
position: point,
map: map,
icon: image
});
var infoWindow = new google.maps.InfoWindow(); //initialize infoWindow
// Add a click event to each marker which will open the HTML window
marker.infowindow = new google.maps.InfoWindow({
content: markerhtml
});
google.maps.event.addListener(marker, "click", function() {
marker.infowindow.open(map, marker);
});
};//end create marker