开发者

Multiple Google map with php loop

I am trying to dispaly multiple maps with a php loop but i cant get it to work here is the code stripped down...

    <!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>Google Maps JavaScript API Example</title>
    <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=true&amp;key=ABQIAAAA5iXFaQoABBiRl7JNx5HFuxSa5RD4FXABRIVtLveK1-E6gmai7BR1Y63hhzwAO9ZPoNDgLDBQ_Z6B4Q" type="text/javascript">开发者_运维百科;</script>


  </head>
  <body onload="load()" onunload="GUnload()">


    <?php

    if($_POST['select'] == "place"){

    $posts = $facebook->api("/search?q=".urlencode($_POST['search'])."&type=".urlencode($_POST['select'])."&center=".urlencode($_POST['center'])."&distance=".urlencode($_POST['distance'])."");

    //print_r($posts);
    $num = "1";
    $num2 = "1";
    foreach($posts as $v) {

        foreach($v as $v2) { ?> 

        <div>
        <script type="text/javascript">

    //<![CDATA[
    function load() {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map-<?php echo $num++; ?>"));
        map.setCenter(new GLatLng(<?php echo $v2['location']['latitude']; ?>, <?php echo $v2['location']['longitude']; ?>), 13);
      }
    }
    //]]>
         <div id="map-<?php echo $num2++; ?>" style="width: 500px; height: 300px"></div>
        <p><?php echo $v2['location']['latitude']; ?></p>
        <p><?php echo $v2['location']['longitude']; ?></p>
        </div>
        <?php

        }

    }


    }

    }

    ?>



  </body>
</html>

I have tried to put the javascript within the loop but this doesnt work.

Has anyone got any suggestions at the moment it will only show the last map within the loop???

Thanks


I can tell you why it doesn't work. You're redefining the same load() function each time through the loop. So when document loads, and load() gets called, it just displays the last map. By the way, you should also be using google map V3 api instead of V2, but that's a different topic.

Something like this should fix this. First, put this in the <head> section:

<script type="text/javascript">
var loaders = [];
function load() {
    for (var i = 1; i < loaders.length; i++) {
        loaders[i]();
    }
}
</script>

Then change your loop to this:

foreach($posts as $v) {

    foreach($v as $v2) { ?> 

    <div>
    <script type="text/javascript">

//<![CDATA[

loaders[<?php echo $num; ?>] = function() {
  if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("map-<?php echo $num++; ?>"));
    map.setCenter(new GLatLng(<?php echo $v2['location']['latitude']; ?>, <?php echo $v2['location']['longitude']; ?>), 13);
  }
};
//]]>
     <div id="map-<?php echo $num2++; ?>" style="width: 500px; height: 300px"></div>
    <p><?php echo $v2['location']['latitude']; ?></p>
    <p><?php echo $v2['location']['longitude']; ?></p>
    </div>
    <?php

    }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜