3

I'm attempting to make a Google Map which allows the user to check off a category and have those specific locations display. I've got that part working:

http://thedenvillehub.com/test-hs/adv/scripts/test.html

What I want to do is have a different icon come up for each category, so that one is red, one is blue, etc. I found a couple of examples online but nothing seems to be working for me. Here's my code for reference:

var markers = new Array();
var locations = [
    ['Boonton Town', '402-9410', 'Dial-a-Ride', 40.902, -74.407, 1 ],
    ['Boonton Township', '331-3336', 'Dial-a-Ride', 40.933, -74.425, 2 ],
    ['Butler Borough', '835-8885', 'Dial-a-Ride', 40.999497, -74.346326, 3 ]
    //other locations and categories
];

var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 10,
    center: new google.maps.LatLng(40.7967667, -74.4815438),
    mapTypeId: google.maps.MapTypeId.ROADMAP
});

var infowindow = new google.maps.InfoWindow();
var marker, i;

for (i = 0; i < locations.length; i++) {  
  marker = new google.maps.Marker({
      position: new google.maps.LatLng(locations[i][3], locations[i][4]),
      map: map,
      icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png'
  });

  markers.push(marker);
  google.maps.event.addListener(marker, 'click', (function(marker, i) {
      return function() {
          infowindow.setContent(locations[i][0] +
              "<br />"+locations[i][2]+"<br />"+locations[i][1]);
          infowindow.open(map, marker);
      }
  })(marker, i));
}

// shows all markers of a particular category,
// and ensures the checkbox is checked

function show(category) {
    for (var i=0; i<locations.length; i++) {
        if (locations[i][2] == category) {
            markers[i].setVisible(true);
        }
    }
}

// hides all markers of a particular category,
// and ensures the checkbox is cleared

function hide(category) {
    for (var i=0; i<locations.length; i++) {
        if (locations[i][2] == category) {
            markers[i].setVisible(false);
        }
    }
}

// == show or hide the categories initially ==

hide("Dial-a-Ride");
hide("American Legion");
hide("Veterans of Foreign Wars");
hide("Nutrition");

$(".checkbox").click(function(){
    var cat = $(this).attr("value");

    // If checked
    if ($(this).is(":checked")) {
        show(cat);
    }
    else {
        hide(cat);
    }
});
Sean Mickey
  • 7,618
  • 2
  • 32
  • 58
Jamie
  • 71
  • 1
  • 6

1 Answers1

6

Make an object var iconSrc = {}

Then, match categories to icon images to it.

iconSrc['Dial-a-ride'] = 'http://labs.google.com/ridefinder/images/mm_20_red.png';
iconSrc['American Legion'] = 'http://labs.google.com/ridefinder/images/mm_20_green.png';
...

when adding a marker, replace the icon image source with the new variable:

icon: iconSrc[locations[i][2]]

Here's a jsfiddle, the navigation is tough in the small pane so I moved the checkboxes to the top. http://jsfiddle.net/DA92S/1/

Heitor Chang
  • 6,038
  • 2
  • 45
  • 65
  • Ah wow, I was thinking it'd be something like that but wasn't sure how to do it. Thank you so much!!! – Jamie May 03 '12 at 16:51
  • hmm. do you have any idea why the infowindow graphics are coming up strangely? http://thedenvillehub.com/test-hs/adv/map.asp – Jamie May 03 '12 at 16:52
  • It looks like there's CSS in the infowindow but I'm not finding the link or references. Can you point it out to me? – Heitor Chang May 03 '12 at 17:04
  • @Jamie I noticed the zoom control is odd too, and found this link: http://stackoverflow.com/q/8511436/1238040 Also please accept my answer if you liked it (click the check mark outline) :) – Heitor Chang May 03 '12 at 18:53
  • ah looks like i found the issue. in 1140.css, there's a property 'img: max-width:100%;' and for whatever reason google maps didnt like that. once i got rid of that, it worked fine. – Jamie May 03 '12 at 18:55
  • Good work. I have a observations and a question: Observation => The 66 point is in Kyrgyszstan, not in USA (['Chatham Borough','Church of Christ, 382 Fairmount Avenue
    (973) 635-1508; open Tuesday and Thursday', 'Nutrition', 40.7227380172141, 74.40338373184204,66 ]). Question => How center this map with bound, not with this line (center: new google.maps.LatLng(40.7967667, -74.4815438))?
    – phsaires Jun 10 '13 at 17:56
  • @HeitorChang How to achieve the same feature with Places Service. I have different categories coming from Places service . – Nancy Mar 01 '19 at 06:07