0

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

1 回答 1

0

试试下面的代码。不同之处在于,我将调用 $.post 之后的所有 statemetns 放置在匿名函数内,该函数在发布成功后才被触发。当你调用 $.Post 时,内部的函数在 post 完成之前不会运行,但在 post 发生在后台时仍会继续执行以下语句!

释义内部匿名函数已扩展为包含大部分代码。如果您不确定差异,请尝试使用 WinMerge 之类的程序,它会显示差异:winmerge 显示差异

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
                    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 post

};//end create marker
于 2012-05-25T17:56:57.550 回答