谷歌地图/ JavaScript - 显示某些标记 - MySQL后端

时间:2014-02-05 16:23:11

标签: javascript google-maps

我有一个在Google地图上显示标记的网页。 标记存储在MySQL DB中。 通过PHP将值检索到XML文件中。

完整说明如下: https://developers.google.com/maps/articles/phpsqlajax_v3

到目前为止一切顺利: http://www.pizzazzle.co.uk/maps/phpAndMySQL.php

我为我的两个“类型”的针添加了两个勾选框。

我想只显示属于被检查类型的相关标记。

因此,如果勾选所有类型,则显示所有引脚。如果只勾选“bar”,则只显示类型为“bar”的引脚。

我正试图弄清楚如何使其发挥作用。主要是因为我不熟悉JavaScript - 我真的是一个PHP人。

任何想法都会很棒。

目前的页面代码如下。我现在已经添加了完整的工作代码。

<!DOCTYPE html >
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>PHP/MySQL & Google Maps Example</title>
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
    <script type="text/javascript">
    //<![CDATA[

//set up globals
var gmarkers = [];
var infoWindow = [];

//set up icons
var customIcons = {
restaurant: {
icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png'
},
bar: {
icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png'
}
};


//load function
function load() {

//initialise map
var map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(47.6145, -122.3418),
zoom: 13,
mapTypeId: 'roadmap'
});
var infoWindow = new google.maps.InfoWindow;
//ok



//set up pins from xmlgen.php file
  // Change this depending on the name of your PHP file
downloadUrl("xmlgen.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 icon = customIcons[type] || {};

    var marker = new google.maps.Marker({
      map: map,
      position: point,
      icon: icon.icon
    });
    marker.mycategory = type;
    gmarkers.push(marker);

    bindInfoWindow(marker, map, infoWindow, html);
}
  });
}

    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() {}


// == shows all markers of a particular category, and ensures the checkbox is checked ==
 function show(category) {
   for (var i=0; i<gmarkers.length; i++) {
     if (gmarkers[i].mycategory == category) {
       gmarkers[i].setVisible(true);
     }
   }
   // == check the checkbox ==
   document.getElementById(category+"box").checked = true;
 }

 // == hides all markers of a particular category, and ensures the checkbox is cleared ==
 function hide(category) {
   for (var i=0; i<gmarkers.length; i++) {
     if (gmarkers[i].mycategory == category) {
       gmarkers[i].setVisible(false);
     }
   }
   // == clear the checkbox ==
   document.getElementById(category+"box").checked = false;
   // == close the info window, in case its open on a marker that we just hid
   infoWindow.close();
 }

 // == a checkbox has been clicked ==
      function boxclick(box,category) {
        if (box.checked) {
          show(category);
        } else {
          hide(category);
        }
      }

    //]]>

  </script>

  </head>

<body onload="load()">

<div id="map" style="width: 700px; height: 500px"></div>

<form action="#">
<input type="checkbox" id="restaurantbox" onclick="boxclick(this,'restaurant')" checked/>
<label>restaurant</label>
<input type="checkbox" id="barbox" onclick="boxclick(this,'bar')" checked/>
<label>bar</label>
</form>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

您目前的问题是,您的代码中唯一的“标记”是downloadUrl回调函数的本地,它是来自的xml对象数组:

var markers = xml.documentElement.getElementsByTagName(“marker”);

boxclick函数是一个HTML onclick函数,在全局范围内运行,你需要一个google.maps.Marker对象数组(全局范围)来调用hide / show on。

  1. 添加全局gmarkers数组(在任何函数之外):

    var gmarkers = [];
    var infoWindow = new google.maps.InfoWindow({}); // update, global infoWindow
    
  2. 在创建它们时将google.maps.Markers推送到该阵列:

    // Change this depending on the name of your PHP file
    downloadUrl("xmlgen.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 icon = customIcons[type] || {};
    
        var marker = new google.maps.Marker({
          map: map,
          position: point,
          icon: icon.icon
        });
        marker.mycategory = type;
        gmarkers.push(marker);
    
        bindInfoWindow(marker, map, infoWindow, html);
    }
    

  3. 更改隐藏和显示函数以使用gmarkers数组:

    // == shows all markers of a particular category, and ensures the checkbox is checked ==
     function show(category) {
       for (var i=0; i<gmarkers.length; i++) {
         if (gmarkers[i].mycategory == category) {
           gmarkers[i].setVisible(true);
         }
       }
       // == check the checkbox ==
       document.getElementById(category+"box").checked = true;
     }
    
     // == hides all markers of a particular category, and ensures the checkbox is cleared ==
     function hide(category) {
       for (var i=0; i<gmarkers.length; i++) {
         if (gmarkers[i].mycategory == category) {
           gmarkers[i].setVisible(false);
         }
       }
       // == clear the checkbox ==
       document.getElementById(category+"box").checked = false;
       // == close the info window, in case its open on a marker that we just hid
       infoWindow.close();
     }