2014-02-05 81 views
0

我有一個網頁在Google Map上顯示標記。 標記存儲在MySQL數據庫中。 通過PHP將這些值檢索到XML文件中。谷歌地圖/ JavaScript - 顯示某些標記 - MySQL後端

完整說明在這裏: 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> 
+0

的[經典的 「類別」 地圖(http://www.geocodezip.com/v3_MW_example_categories.html)(從翻譯到v3 [麥克Williams的V2教程](HTTP:// econym .org.uk/gmap/categories.htm)) – geocodezip

+0

@geocodezip感謝您對本文的建議。我添加了onclick事件和他們所調用的功能。我得到一個控制檯錯誤,說'標記'沒有定義。我認爲它談論'隱藏'和'顯示'功能中的標記。 http://www.pizzazzle.co.uk/maps/phpAndMySQL-with%20checkboxes.html – metaBaron

回答

0

你目前的問題是,在你的代碼的唯一「標誌」是本地的將downloadURL回調函數,它是XML對象的數組來自:

VAR的標記= xml.documentElement .getElementsByTagName( 「標誌物」);

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. 更改隱藏和顯示功能以使用gmarkkers數組:

    // == 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(); 
    } 
    
+0

謝謝@geocodezip。我已經做出了您所建議的更改。我現在得到一個GET錯誤。沒有比這更多的信息。現場演示:http://pizzazzle.co.uk/maps/phpAndMySQL-with%20checkboxes2.html – metaBaron

+0

...我忘了提及,我已經更新了頂部的代碼,以重新載入您建議的更改@geocodezip – metaBaron

+0

我的答案有一個錯字(infowindow應該是infoWindow),並且沒有將「mycategory」屬性添加到標記。固定。 – geocodezip