Multiple markers with labels in Google Maps
21:00 20 Jan 2011

I'd like to implement multiple markers with labels (documented here: http://google-maps-utility-library-v3.googlecode.com/svn/tags/markerwithlabel/1.0.1/docs/examples.html) and I'm having some problems with writing the code.

For 2 markers for example you'd have to use the following code:

var latlng1 = new google.maps.LatLng(49, -123);
var latlng2 = new google.maps.LatLng(48, -123);

var marker1 = new MarkerWithLabel({
       position: latlng1,
       draggable: true,
       map: map,
       labelContent: "abcd",
       labelAnchor: new google.maps.Point(22, 0),
       labelClass: "labels",
       labelStyle: {opacity: 0.75}
     });

 var marker2 = new MarkerWithLabel({
       position: latlng2,
       draggable: true,
       map: map,
       labelContent: "efgh",
       labelAnchor: new google.maps.Point(22, 0),
       labelClass: "labels",
       labelStyle: {opacity: 0.75}
     });

This is OK, but what about 10 markers? I'd like to be able to do something like this:

var locations = [
    new google.maps.LatLng(49, -123),
    new google.maps.LatLng(48, -123),
    ];

var labels = [
    "abcd",
    "efgh",
    ];

The problem is that when I write a "for" function, the map doesn't display any markers.

for ( var i = 0; i < locations.length; i++ )
{
    var marker = new MarkerWithLabel({
        position: locations[i],
        draggable: true,
        labelContent: labels[i],
        labelAnchor: new google.maps.Point(22,0),
        labelClass: "labels",
        labelStyle: {opacity: 0.85}});
}

I'm not very good at programming, so, can you please tell me what I'm doing wrong?

After this, of course there's the issue of adding info windows, but let's talk about that later.

Thank you for your time.

google-maps google-maps-markers label