从数据库中显示标记

时间:2014-12-01 14:32:04

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

以下java script会在google地图中显示标记。这里我添加了Control 按钮到LEFT_BOTTOM。我的出局显示所有马克。但是当我点击Control按钮时,我需要从数据库中过滤一些标记。我应该怎么做?

 <style type="text/css">
            body { font: normal 10pt Helvetica, Arial; }
            #map { width: 100%; height: 100%; border: 0px; padding: 0px; }
    </style>
    <script src="http://maps.google.com/maps/api/js?v=3&sensor=false" type="text/javascript"></script>
    <script type="text/javascript">
        var icon = new google.maps.MarkerImage("images/icons/icon.png",
                       new google.maps.Size(32, 32), new google.maps.Point(0, 0),
                       new google.maps.Point(16, 32));
        var center = null;
        var map = null;
        var currentPopup;
        var bounds = new google.maps.LatLngBounds();

        //adding multiple marker
        function addMarker(lat, lng, info) {
            var pt = new google.maps.LatLng(lat, lng);
            bounds.extend(pt);
            var marker = new google.maps.Marker({
                position: pt,
                icon: icon,
                map: map});
            var popup = new google.maps.InfoWindow({
                content: info,
                maxWidth: 300 });
            google.maps.event.addListener(marker, "click", function() {
                if (currentPopup != null) {
                    currentPopup.close();
                    currentPopup = null;
                }
                popup.open(map, marker);
                currentPopup = popup;});
            google.maps.event.addListener(popup, "closeclick", function() {
                map.panTo(center);
                currentPopup = null;
            });
        }
        function HomeControl(controlDiv, map) {

          // Set CSS styles for the DIV containing the control
          // Setting padding to 5 px will offset the control
          // from the edge of the map
          controlDiv.style.padding = '5px';

          // Set CSS for the control border
          var controlUI = document.createElement('div');
          controlUI.style.backgroundColor = 'white';
          controlUI.style.borderStyle = 'solid';
          controlUI.style.borderWidth = '2px';
          controlUI.style.cursor = 'pointer';
          controlUI.style.textAlign = 'center';
          controlUI.title = 'Click to Filter';
          controlDiv.appendChild(controlUI);

          // Set CSS for the control interior
          var controlText = document.createElement('div');
          controlText.style.fontFamily = 'Arial,sans-serif';
          controlText.style.fontSize = '12px';
          controlText.style.paddingLeft = '4px';
          controlText.style.paddingRight = '4px';
          controlText.innerHTML = '<b>GYM</b>';
          controlUI.appendChild(controlText);

          // Setup the click event listeners: simply set the map to
          google.maps.event.addDomListener(controlUI, 'click', function() {     

                //need to filter Markers
                //$query = mysql_query("SELECT * FROM mytable where availability = false");


          });
        }
        function initMap() {
            map = new google.maps.Map(document.getElementById("map"), {
                center: new google.maps.LatLng(0, 0),
                zoom: 14,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                mapTypeControl: false,
                mapTypeControlOptions: {
                    style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR
                },
                navigationControl: true,
                navigationControlOptions: {
                    style: google.maps.NavigationControlStyle.SMALL}});
            <?php
                $query = mysql_query("SELECT * FROM mytable ");
                while ($row = mysql_fetch_array($query)){
                    $regno      =   $row['id'];
                    $latitude   =   $row['latitude'];
                    $longitude  =   $row['longitude'];  
                    echo "addMarker($longitude, $latitude,'<b>$id</b>');";
                }
            ?>
            center = bounds.getCenter();
            map.fitBounds(bounds);

            var homeControlDiv = document.createElement('div');
            var homeControl = new HomeControl(homeControlDiv, map);
            homeControlDiv.index = 1;
            map.controls[google.maps.ControlPosition.LEFT_BOTTOM].push(homeControlDiv);

        }
    </script>

----
<body onLoad="initMap()" style="margin:0px; border:0px; padding:0px;">

1 个答案:

答案 0 :(得分:0)

  1. 创建标记时,将它们推送到数组(按下每个标记对象)。
  2. 点击按钮,循环浏览标记数组并在每个标记上调用setMap(null)
  3. 使用过滤器
  4. 再次查询您的数据库

    这是一些快速代码,以便您明白:

    // Create an array of markers
    var markers = [];
    
    // Create your map and all the stuff you need
    
    // Bind the event listener
    google.maps.event.addDomListener(controlUI, 'click', function() {
    
        // First delete all your markers
        deleteMarkers();
    
        // Query your database again and add each marker as you did in the first place
    });
    
    // Function to delete the markers
    function deleteMarkers() {
    
        for (var i = 0; i < markers.length; i++) {
    
            // Remove each marker from map
            markers[i].setMap(null);
        }
    }
    
    function addMarker() {
    
        // Create each marker object
    
        var marker = new google.maps.Marker({
            map: map,
            // Your other marker properties here
        });
    
        // Push new marker to the markers array
        markers.push(marker);
    }