jpost最后执行?

时间:2012-05-25 16:58:53

标签: jquery google-maps post

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

1 个答案:

答案 0 :(得分:0)

尝试以下代码。差异在于我在匿名函数内部调用$ .post之后放置所有statemetns,该函数在之后触发并且仅在帖子成功时触发。当你调用$ .Post时,内部的函数在帖子完成之前不会运行,但是当帖子在后台发生时执行仍然继续执行以下语句!

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

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