动态downloadURL()调用

时间:2015-05-15 14:18:18

标签: javascript php google-maps google-maps-api-3 mysqli

我正在建立一个网站,其中包括照片库功能。在图库页面中,有一个带有多个标记的地图(谷歌),每个标记都与特定照片相关。

我的数据库中有一个'markers'表:

markers:  
id      | name     |  address | lat   | lng | gallery_id
_______________________________________________________
1       | ..       | ....     |..     | ..  | 2   
2       | ..       | ....     |..     | ..  | 2 
3       | ..       | ....     |..     | ..  | 3
4       | ..       | ....     |..     | ..  | 3
5       | ..       | ....     |..     | ..  | 10
...................... 

所有字段都填充了地理定位数据,每行代表用户上传的特定照片,除了gallery_id之外的所有照片,其中包含拥有照片的图库的ID。

在图库页面中有用于构建谷歌地图的JavaScript:

<script type="text/javascript">

    function load() {
      var map = new google.maps.Map(document.getElementById("map"), {
        disableDefaultUI: true,
        scrollwheel: false,
        navigationControl: false,
        mapTypeControl: false,
        scaleControl: false,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      });
      var infoWindow = new google.maps.InfoWindow;
      var bounds = new google.maps.LatLngBounds();

      //phpsqlajax-xmlgenerator.php, get the data from database
      downloadUrl("phpsqlajax-xmlgenerator.php", function(data) {
        var xml = data.responseXML;
        var markers = xml.documentElement.getElementsByTagName("marker");
        for (var i = 0; i < markers.length; i++) {
          var name = markers[i].getAttribute("name");
          var address = markers[i].getAttribute("address");
          var type = markers[i].getAttribute("type");
          var point = new google.maps.LatLng(
              parseFloat(markers[i].getAttribute("lat")),
              parseFloat(markers[i].getAttribute("lng")));
          var html = "<b>" + name + "</b> <br/>" + address;
          var marker = new google.maps.Marker({
            map: map,
            position: point,
            icon: icon.icon,
            animation: google.maps.Animation.BOUNCE
          });
          bindInfoWindow(marker, map, infoWindow, html);
          bounds.extend(marker.position);
        }
        map.fitBounds(bounds);
      });
    }

    function bindInfoWindow(marker, map, infoWindow, html) {
      google.maps.event.addListener(marker, 'click', function() {
        infoWindow.setContent(html);
        infoWindow.open(map, marker);
      });
    }

    function downloadUrl(url, callback) {
      var request = window.ActiveXObject ?
          new ActiveXObject('Microsoft.XMLHTTP') :
          new XMLHttpRequest;

      request.onreadystatechange = function() {
        if (request.readyState == 4) {
          request.onreadystatechange = doNothing;
          callback(request, request.status);
        }
      };

      request.open('GET', url, true);
      request.send(null);
    }

    function doNothing() {}
  </script>  

我提供了下载URL phpsqlajax-xmlgenerator.php文件,我用它来构建包含标记表数据的xml文件:

<?php
//Start xml file, create parent node
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);

// Opens a connection to a MySQL server
$conn = mysqli_connect('localhost', 'root', 'password', 'database');
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

//Select rows in the markers table
$query = "
        SELECT * FROM `markers` WHERE 1;
";
$result = mysqli_query($conn, $query);

header("Content-type: text/xml");

// Iterate through the rows
while ($row = mysqli_fetch_assoc($result)){
  // ADD TO XML DOCUMENT NODE
  $node = $dom->createElement("marker");
  $newnode = $parnode->appendChild($node);
  $newnode->setAttribute("name",$row['name']);
  $newnode->setAttribute("address", $row['address']);
  $newnode->setAttribute("lat", $row['lat']);
  $newnode->setAttribute("lng", $row['lng']);
  $newnode->setAttribute("type", $row['type']);
}
echo $dom->saveXML();
?>  

到目前为止一切正常,只要数据库中只有一个图库,地图和标记就是正确的。
现在出现了我的问题:我需要一种方法来向phpsqlajax-xmlgenerator.php提供gallery_id,以便执行正确的查询,因为旧的只返回标记的每一行,无论图库。我想将phpsqlajax-xmlgenerator.php代码放在gallery.php页面的顶部并直接在页面上生成xml文件,但是我应该为downloadURL函数提供什么? gallery.php?我想到的另一种方法是在downloadURL参数中添加某种$_GET请求,但我找不到正确执行该方法的方法。

1 个答案:

答案 0 :(得分:1)

将gallery_id与URL一起传递,例如:

downloadUrl("phpsqlajax-xmlgenerator.php?id=3", function(data) {

并根据get-parameter构建sql:

$id=@intval($_GET['id']);
if($id<1){
  exit();
}
$query = 'SELECT * FROM `markers` WHERE gallery_id='.$id;