我有一個網頁在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>
的[經典的 「類別」 地圖(http://www.geocodezip.com/v3_MW_example_categories.html)(從翻譯到v3 [麥克Williams的V2教程](HTTP:// econym .org.uk/gmap/categories.htm)) – geocodezip
@geocodezip感謝您對本文的建議。我添加了onclick事件和他們所調用的功能。我得到一個控制檯錯誤,說'標記'沒有定義。我認爲它談論'隱藏'和'顯示'功能中的標記。 http://www.pizzazzle.co.uk/maps/phpAndMySQL-with%20checkboxes.html – metaBaron