Twitter: cfjedimaster


Address: Lafayette, LA, USA

Simple Google Maps demo with Custom Markers

12-01-2012 6,805 views JavaScript 28 Comments

This isn't anything special, but a reader today asked if I could modify some code I did for an earlier blog post: Simple introduction to Google Maps - Part 2 - Markers. This was a blog post I had done as part of a series introducing folks to using the Google Maps SDK. While partially focused on ColdFusion (and replacing <cfmap>), it was applicable to anyone who wanted to try out the SDK on their web site.

The final demo in that blog post (available here) showed how to add dynamic markers to a map and provided a simple UI to select and focus on each marker.

My reader had two main questions. First, he wanted to know how to customize the marker. Second, he wanted to build in a basic filtering system based on a type of content. He was building a map of medical related locations and he wanted to be able to filter by pharmacies or hospitals. I whipped up a demo of both of these concepts and I thought folks might like to see the code.

Let's begin by talking about custom markers. This is really an incredibly trivial thing to do. While you have a few different options for customizing the marker, the simplest way to do so is to point to a URL. So given this code to create a marker:

You can tell Google to use a custom image like so:

In this case, I'm pointing to a file called drugstore.png that exists on my server. If you google for "google map icons", you'll find a great selection of icons out there, many free to use. I found a great set here: http://mapicons.nicolasmollet.com/ In fact, he even had a medical category. I grabbed that zip, downloaded it, and expanded it.

To make use of these icons, I took some static code he rewrote and created a more generic version of it. In my code, I had an array of locations. Each location included a title and an address. I then added a "type" property that mapped to the types of things he was looking to filter. This then allowed me to build a utility that could map the medical type to an image file. I also could have allowed for completely custom icons. For example, maybe making use of a hospital's logo. But for now, I went with a simple type to icon solution. Here's how I set up the sample data and the icon "mapping" code. Obviously I made some guesses here and went with a silly choice for the default.

Here's how it looks now:

Filtering was easy too. I used jQuery to bind to changes to the checkboxes on top. I then did a show/hide on both the left hand list of locations and the markers as well. (Google had a simple API to show/hide markers.)

And that's it. A good modification to this would be to use the Google API that 'centers' around markers. I'm not sure if that is smart enough to handle hidden markers, but it could be useful as you filter to adjust the map. (Then again that may be disorienting to users.)

You can view the full demo below.

Related Blog Entries

28 Comments

  • SuperAlly #
    Commented on 12-01-2012 at 3:57 PM
    Thanks Ray. I'm in the middle of this very thing, and this will be a big help.
  • SuperAlly #
    Commented on 12-01-2012 at 11:25 PM
    Ray
    This was a great help. One thing I noticed when putting my own map together, when you click on a link in the sidebar, it closes the previous infowindow, but if you click on the map markers themselves, it leaves previous infowindows open.
    Any idea how to work around that? I thought calling infowindow.close() before infowindow.open would suffice, but no luck.
  • SuperAlly #
    Commented on 12-02-2012 at 12:00 AM
    Ray, my apologies for the repeated posts, but I got the infowindows working properly by changing this:
    google.maps.event.addListener(marker, 'click', function() {
    infowindow.open(map,marker);
    });

    to this slight mod of the other block of code:
    google.maps.event.addListener(marker, 'click', function() {
    if(lastinfowindow instanceof google.maps.InfoWindow) lastinfowindow.close();
    marker.infowindow.open(map, marker);
    lastinfowindow = marker.infowindow;
    });

    Just wanted to pass it along, and thanks again.
  • Misty #
    Commented on 12-02-2012 at 1:03 AM
    Hi ray, I had build this and filtering a long but with xml and javascript . cfmap was not introduced then. was pretty easy the way. But you make nice usage and it looks awesome

    Cheers
  • Commented on 12-02-2012 at 9:16 AM
    No need to apologize, SuperAlly, thanks for finding that *and* fixing it.
  • Paul #
    Commented on 12-03-2012 at 12:04 PM
    I love the function and the code, but I think the people of Denver or Sante Fe may not like their new locations ;)
  • Commented on 12-03-2012 at 12:15 PM
    Paul - sorry - what? Did the geocode fail?
  • Paul #
    Commented on 12-03-2012 at 12:59 PM
    It was on the first demo, orders 5 & 6.
  • Commented on 12-03-2012 at 1:01 PM
    Interesting. Definitely a bug there. Well, it's old, so I'm not going to worry about it. ;)
  • Commented on 12-03-2012 at 4:30 PM
    Follow up post with SuperAlly's fix in it:

    http://www.raymondcamden.com/index.cfm/2012/12/3/S...
  • Commented on 01-14-2013 at 9:21 AM
    I an a novice to joomla and am not a programmer. I do my own websites and have been looking for a plugin or module that would allow me to reference google icons in a menu; in other words, have a menu with an image that matches an icon on a map. Do you have any suggestions?
  • Commented on 01-14-2013 at 9:22 AM
    For Joomla? I don't use it. Sorry.
  • Hicham #
    Commented on 01-22-2013 at 5:42 AM
    Hi Raymond Camden,

    Thank you so much for this code, exactly what i was searching for :)
    Is it possible to ad a mailto: link for email in the map ?
  • Commented on 01-22-2013 at 6:25 AM
    The title values for those markers can include HTML - just use it there.
  • denis #
    Commented on 04-12-2013 at 2:32 PM
    I'm trying to building a web app using google maps, and seeing the aplicatios of the demo, it's what I'm finding, I'm studing the code e creating my work!But now, the mission is to create an application where people clicks on checkbox e just show the icons by category. Adress isn't necessary in my context. What class or library I should to use to finish the web app? I'm using in this application javascript and html5
  • Commented on 04-13-2013 at 3:04 PM
    Um. I don't know if I really understand you. I believe you want JavaScript but honestly I can't tell.
  • Lara #
    Commented on 04-15-2013 at 2:30 AM
    Hi, i'm trying to building a web site for the people create markers about crime in a city. Do you know if can i open a box for it? Save in a database and load after?
  • Commented on 04-15-2013 at 6:02 AM
    "Do you know if can i open a box for it? Save in a database and load after?" Not sure what you mean by open a box. If you mean, "Can I allow people to add markers (with some text data too) and store it" then the answer is yes.
  • Cruelcrome #
    Commented on 04-17-2013 at 2:06 AM
    Any1 know if I want to zoom/bound to only the active markers(by the checkboxes) and fit the map after them?
    ... And if no checkboxes are selected then go back to default map zoom/center ?? .
  • Commented on 04-17-2013 at 6:28 AM
    "Any1 know if I want to zoom/bound to only the active markers(by the checkboxes) and fit the map after them?" Afaik, the Google Maps API has the ability to zoom/fit things, so this _should_ be possible.
  • Cruelcrome #
    Commented on 04-18-2013 at 3:07 AM
    ok, ty i'll try that...

    do you have any clue on how i highlight(by chance the loc class somehow) so i can see which marker is active at the sidebar?

    regards Cruelcrome
  • Commented on 04-18-2013 at 6:25 AM
    Nope. I'd recommend going to the docs. :)
  • denis #
    Commented on 04-19-2013 at 3:28 PM
    Yes. Javascript is tme main language that i'm using. But, I'm tring to use jQuery to show and hide markers in the map based on the checkboxes that are selected.
  • Commented on 04-19-2013 at 3:31 PM
    Which is what I demoed, right?
  • denis #
    Commented on 04-25-2013 at 5:27 PM
    Yes, I saw this demo , that gave me an orientation to building in an web application
  • Commented on 04-25-2013 at 6:50 PM
    Sorry, but what exactly are you trying to ask me?
  • denis #
    Commented on 04-30-2013 at 6:06 PM
    Ok, let's go. I'm using a web application that shows in a map the icons , using google maps . For this,I want using a <select> tag to filter the icons by categories, instead of checkboxes. I hope that you undestad!
  • Commented on 04-30-2013 at 9:31 PM
    So, step one then would be for you to add a dropdown and notice the change. Can you handle that portion? That's simple jQuery event handling.

Post Reply

Please refrain from posting large blocks of code as a comment. Use Pastebin or Gists instead.

Leave this field empty