I have a problem that I have not managed to solve.
I developed a small script to load a Google Map. This script uses HTML 5 to determine your current location and then shows you some places on the map. These are places that are read from a MySQL database.
The issue is that somehow I need to filter them by category. A PHP file is responsible for generating an XML file from the MySQL database.
I could pass the following parameter to select the category: file.php?category=something
downloadUrl("file.php?cat=something", function(data) {
var markers = data.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var latlng = new google.maps.LatLng(parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var marker = createMarker(markers[i].getAttribute("name"), markers[i].getAttribute("address"), markers[i].getAttribute("type"), latlng);
}
});
The issue is that I want to go changing category when I click on a link for example.
Imagine you have a map to the left and right categories. Then when clicking on a category, the map automatically remove the places marked on the current category and only shows the new category.
In conclusion, the part where it says file.php? Category = something, the value “something” should be changed dynamically.
I think it could do with Jquery but I do not know exactly how. I tried several ways and still not do it.
I just need a way to change the value of this part and that the map will automatically disable the other categories, for example:
var something = doctor;
downloadUrl("file.php?cat="+something, function(data) {
This is the complete code of my script:
<!DOCTYPE html>
<html>
<head>
<title>Geolocalización</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="UTF-8">
<link href="estilo.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.js"></script>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=true"></script>
<script type="text/javascript" src="util.js"></script>
</head>
<body onload="initialize()">
<div id="info"></div>
<div id="map_canvas" style="width: 780px; height: 600px; margin: 0 auto;"></div>
<script type="text/javascript">
var map;
var infowindow;
var geocoder;
var marker;
function initialize() {
var myOptions = {
zoom: 14,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);
downloadUrl("phpsqlajax_genxml.php?cat=doctor", function(data) {
var markers = data.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var latlng = new google.maps.LatLng(parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var marker = createMarker(markers[i].getAttribute("name"), markers[i].getAttribute("address"), markers[i].getAttribute("type"), latlng);
}
});
function createMarker(name, address, type, latlng) {
if(type=="doctor"){
var image = 'clinicas-y-hospitales.png';
}else{
var image = 'carabineros-y-seguridad.png';
}
var contentString = '<h2>'+name+'</h2> <br /><p>'+address+'</p>';
var marker = new google.maps.Marker({position: latlng, map: map, icon: image});
google.maps.event.addListener(marker, "click", function() {
if (infowindow) infowindow.close();
infowindow = new google.maps.InfoWindow({content: contentString, maxWidth: 200});
infowindow.open(map, marker);
});
return marker;
}
geocoder = new google.maps.Geocoder();
// Try HTML5 geolocation
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude);
map.setCenter(pos);
geocoder.geocode({'latLng': pos}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
marker = new google.maps.Marker({
position: pos,
map: map
});
infowindow = new google.maps.InfoWindow({content: '<p><strong>Usted está aquí:</strong> '+results[1].formatted_address+'</p>'});
infowindow.open(map, marker);
document.getElementById('info').innerHTML = results[1].formatted_address;
}
} else {
alert("Geocoder failed due to: " + status);
}
});
}, function() {
handleNoGeolocation(true);
});
} else {
// Browser doesn't support Geolocation
handleNoGeolocation(false);
}
}
function handleNoGeolocation(errorFlag) {
if (errorFlag) {
var content = 'Error: La geolocalización ha fallado.';
} else {
var content = 'Error: Tu navegador no soporta geolocalización.';
}
var options = {
map: map,
position: new google.maps.LatLng(60, 105),
content: content
};
var infowindow = new google.maps.InfoWindow(options);
map.setCenter(options.position);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</body>
</html>
To see the script running are here:
http://pruebas.davidbanner.cl/geo/index.php
A similar example of what I need to do is here:
http://mapas.vinadelmarchile.cl/index.php
Thank you very much to anyone who can give me an idea or help with code.
Greetings.
You should try to let’s say put a button or link and on the click event implement the call to get the xml.
If you need further assistance please make a working example in jsfiddle (example) and post it here in the comments so we can take a look.