我有一个创建谷歌地图标记的功能。我从数据库查询中传递数据。然后,我让函数使用一个值作为外键执行另一个数据库查询。然后,我试图将所有结果(第一个查询的数据和第二个查询的结果)放入一个字符串中,标记将在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