Adding Markers on Google Maps (with more functionalities)

I modified my previous markers example to show off further functionalities:

  • To pop up an info window with marker description.
  • To handle clicks and create new markers.
  • To use markers with custom images.

Extended Google Map Markers

To pop up an info window with marker description
Google maps handles clicks through event listeners. Thus, to open the info window we use the GEvent object:

GEvent.addListener(marker, 'click',
      function()
      {
         var htmlString = "<font color=\"#6387A5\" size=\"1\"
                                 face=\"Verdana, Arial, Helvetica, sans-serif\">"+
                          description+"<br/>" +
                          "(" + latitude + "," + longitude + ")</font>";
         marker.openInfoWindowHtml(htmlString);
      }
   );

The addListener method takes your marker, event type, and a callback function. The callback function is executed when the event is fired, and it calls marker.openInfoWindowHtml which opens an info window.

We also pass a string with the openInfoWindowHtml method, which will be displayed in the info window.

To handle clicks and create new markers
We also handle clicks on the map with GEvent, but this time we add a listener by passing the map instead of the marker:

GEvent.addListener(map, "click", function(overlay, location) {

         if( overlay )
         {
            // clicked on something other than the map

         }
         else
         {
            var description = prompt("Please enter marker description", "");

            // create a new marker
            if( description != null )
               addMarker(location,description);
         }

      });

I had an issue that triggered the map event when clicking on an info window. Ideally we should be able to tell where the click came from, and handle the event appropriately. This is possible with the callback function taking two arguments: an overlay, and the coordinates of the click.

The first argument is the overlay that was clicked by the user; for example markers and info windows. Therefore, when the first argument is not an overlay we can safely say that the user clicked on the map.

To use markers with custom images

We use the GIcon object to create markers with custom images. Once we create an instance of GIcon we pass it into the GMarker constructor; displaying the custom image instead of the default marker.

In the example we randomly use one of the three custom markers:
Green MarkerBlue MarkerYellow Marker

// create new gicon to use with the marker
var customIcon = new GIcon();

// put in random image
var pos = Math.floor(Math.random()*3);
var images = new Array("images/marker_green.png",
"images/marker_blue.png",
"images/marker_yellow.png");
customIcon.image = images[pos];

customIcon.iconSize = new GSize(20, 34);
customIcon.iconAnchor = new GPoint(10, 34);
customIcon.infoWindowAnchor = new GPoint(10, 0);

// create new marker
var marker = new GMarker(location,customIcon);

11 Responses to “Adding Markers on Google Maps (with more functionalities)”


  1. 1 Robert Saturday, 23 August 2008 at 11:23 am

    Very helpful tutorial!

    Is it possible to be done with middle click (left click is used to zoom in the map)…?

  2. 2 IT Outsourcing | Web Development | Globalprompt Wednesday, 15 October 2008 at 1:50 am

    I am using custom icon for google map. But I am not able to add action listener to it.

    http://www.caricarimakan.com/printUrl/2

    can someone help me ?

    Thanks

  3. 3 marcelpad Wednesday, 15 October 2008 at 9:09 am

    To add a listener to a marker you need to use the GEvent.addListener() function.

    More info here http://code.google.com/apis/maps/documentation/reference.html#GEvent.

    Here is a quick example:

    var marker = new GMarker(latlong,customIcon);

    GEvent.addListener(marker, ‘click’,
    function()
    {
    // do something on click
    }
    );

    Hope that helps!

  4. 4 Simón Algorta Tuesday, 28 October 2008 at 8:14 am

    I have embeded my map in a jsp and i have a request response servlet as well. ¿where do i write this code? ¿in the servlet?

  5. 5 Simón Algorta Tuesday, 28 October 2008 at 9:19 am

    ok im sorry that was a silly question…

  6. 6 Pol Friday, 15 May 2009 at 8:36 pm

    Hello Marcel,

    Thanks for your post, thanks to you I succeded to add more markers on a map and more infowindow.

    I made a new version of the Google Latitude Badge.

    You can see it live here: http://zetransporter.blogspot.com/2009/05/google-latitude-badge-improved.html

    Tell me what you think !

    Cya :)

  7. 7 Caleb Tan Wednesday, 27 May 2009 at 8:10 am

    Good job! You are the pride of Malaysia :D.

  8. 8 marcelpad Thursday, 28 May 2009 at 6:15 pm

    Hi Pol. I am happy that the post was helpful to you. I am going to go check out your app.

  9. 10 Jobo Tuesday, 6 October 2009 at 1:53 pm

    Thanks for the post. your codes and scripts are very usefull

  10. 11 Peter Thursday, 15 October 2009 at 12:32 am

    I don’t know how to incorporate those 4 lines you give into the entire script.

    Please put the whole chunk of script from start to end tags so I can get started. I’ve got a map working but your code does not produce a marker on my map.

    Thanks


Leave a Reply




Hello

My name is Marcel Tjandraatmadja, and I live in Melbourne, Australia. I have a special interest in PHP and web applications.
I also enjoy problem solving with C#, C, and C++.

My interests are also Aikido, roller blading, and music from my favorite bands.

Curriculum Vitae

Download my CV here.

Contact

Drop me a message at marcel.tjandra@gmail.com.

my del.icio.us

Posts

html hit counter