checkboxes and markers

182 views
Skip to first unread message

Dinos

unread,
May 12, 2011, 6:59:08 AM5/12/11
to Google Maps JavaScript API v3
Hi,

I am making a map in which I want to hide or to show markers when some
checkboxes are checked or not.
The data are loaded from a database with the use of an xml file.
I didn't find something how to do this (show/hide) for v3, so I
decided to try something by myself.
It works, but I am not satisfied by the way it is coded and I would
also like to work better and I will explain what I mean.

Every time that I click a checkbox I have an onclick call to the
initialize() function. Due to this if I have zoomed in the map and
click a checkbox it returns the map with the right data but of course
in the initial zoom.

Now about the code:

jQuery.get("data.xml", {}, function(data) {
jQuery(data).find("marker").each(function() {
var marker = jQuery(this);
var putIt = false;
//checkboxes
var cat_paper = document.getElementById("cat-paper").checked;
var cat_glass = document.getElementById("cat-glass").checked;
var cat_plastic = document.getElementById("cat-plastic").checked;
var cat_aluminium = document.getElementById("cat-
aluminium").checked;
var cat_general = document.getElementById("cat-general").checked;
var latlng = new
google.maps.LatLng(parseFloat(marker.attr("lat")),
parseFloat(marker.attr("lng")));
if(cat_paper && marker.attr("paper") == "1"){
putIt = true;
}
if(cat_glass && marker.attr("glass") == "1"){
putIt = true;
}
if(cat_plastic && marker.attr("plastic") == "1"){
putIt = true;
}
if(cat_aluminium && marker.attr("aluminium") == "1"){
putIt = true;
}
if(cat_general && marker.attr("packet") == "1"){
putIt = true;
}
if(putIt){
var marker = new google.maps.Marker({position: latlng, map:
map});
}
});


The above code works but it seems to me a bit ugly... Could you
propose me something? Also about the first question is there a way to
keep the current zoom? I would appreciate even negative answers.
Sometimes it saves you a lot of time to know that there is no solution
for a problem.


Kind regards,

Dinos

Bob Hamers

unread,
May 12, 2011, 1:10:26 PM5/12/11
to google-map...@googlegroups.com
Hey, you could check out this script; http://www.geocodezip.com/v3_MW_example_categories.html
I'm just implementing it now, and seems to work perfectly.

Dinos

unread,
May 16, 2011, 6:36:04 AM5/16/11
to Google Maps JavaScript API v3
That's exactly what I was looking for.

Thanks!
Reply all
Reply to author
Forward
0 new messages