谷歌地图-Javascript与ASP经典循环不重复

google maps - Javascript with ASP classic loop not repeating

本文关键字:循环 经典 -Javascript ASP 谷歌地图      更新时间:2023-09-26

我在ASP循环中使用以下javascript时遇到了一个小问题

<script type="text/javascript">
var geocoder, location1, location2, gDir;
function initialize() {
    geocoder = new GClientGeocoder();
    gDir = new GDirections();
    GEvent.addListener(gDir, "load", function() {
        var drivingDistanceMiles = gDir.getDistance().meters / 1609.344;
        var drivingDistanceKilometers = gDir.getDistance().meters / 1000;
    var drivingTime = gDir.getDuration().html
        document.getElementById("<%=brokeridnum%>").innerHTML = '<strong>Address 1: </strong>' + location1.address + ' <br /><strong>Address 2: </strong>' + location2.address + '<br /><strong>Driving Distance: </strong>' + drivingDistanceMiles.toFixed(2) + ' miles taking ' + drivingTime;
    });
}
</script>
<script type="text/javascript">

    function showLocation(startpc, endpc) {
        geocoder.getLocations(startpc, function (response) {
            if (!response || response.Status.code != 200)
            {
                alert("Sorry, we were unable to geocode the first address");
            }
            else
            {
                location1 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
                geocoder.getLocations(endpc, function (response) {
                    if (!response || response.Status.code != 200)
                    {
                        alert("Sorry, we were unable to geocode the second address");
                    }
                    else
                    {
                        location2 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
                        gDir.load('from: ' + location1.address + ' to: ' + location2.address);
                    }
                });
            }
        });
    }
    </script>

当循环运行时,javascript只在元素内部显示一次。

<%=brokeridnum%>在JS中插入一个数字,从chrome中查找源代码可以正常工作。

id为brokeridnum的元素也在工作。

<script type="text/javascript">
   initialize();
   showLocation("postcode1","<%=destpc%>");
   </script>
<p id="<%=rsbkr("broker_id")%>"></p>

这就是这些函数被称为的方式

如有任何帮助,我们将不胜感激!

Without seeing the entire script I can only guess...
Maybe instead of looping the entire script, try 
only looping the function call with a   parameter like this:
<% Do   . . . %>
  <script type="text/javascript">
     initialize(<%= brokeridnum %>);
  </script>
<% LOOP . . . %>
The function now outside the loop waits for the brokerid parameter...
function initialize(brokerid){
.....
.....
document.getElementById(brokerid).innerHTML = '......
}
Sorry about abbreviating the code a bit, but I hope I helped.