English 中文(简体)
jpost最后一次表演?
原标题:jpost being performed last?

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

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

释义内部匿名函数已经扩展到包含大部分代码。如果您不确定差异,请尝试使用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
问题回答

暂无回答




相关问题
How to decide the current point reach on google map?

How to decide the current point reach on google map? I have a list of points (pickup points) of a route that I want to show in my google map with polyline. Now i have to get the current location of ...

Topographical or relief data in Map APIs

I was wondering if anyone knew of any map APIs that offer topographical or relief data? I ve had a quick look at Google and Bing APIs, but could find nothing there. Google allow you to view a map as ...

Using maps on Windows Mobile

I m experimenting with maps on different mobile platforms. Getting Google Maps to work on Android was easy, following this tutorial. Getting the same to work on Windows Mobile is a different matter. ...

Adding a custom icon to a google map

I need a hand adding a custom icon to some Google Maps javascript. Code below for your reference: function populateMap() { var map = new GMap2(document.getElementById("map")); map.setCenter(new ...

RSS to KML Overlay

I m want to display my blog as a Google Map overlay (each post contains geotags). How can I dynamically create a KML overlay from an RSS? Or better, how can I create a loop (PHP) that would display ...

开放街道地图管理员

我需要开放Street的标记管理员。 地图,如山角地图。

热门标签