23

I would like to display a text label next to the markers on google maps. I've used Virtual Earth before and I'm just starting to use Google Maps. I tried setting the Title property but that only changes the roll over text.

Is there a way to display a small line of text underneath a marker that will stay there as the user zooms, pans and uses the map?

Thanks in advance!

Drew Noakes
  • 284,599
  • 158
  • 653
  • 723
Great Turtle
  • 3,295
  • 7
  • 31
  • 36
  • I think I may have found an answer... the site appears to be down for me but Google has a cache of it. I'll try this out and see how it works. http://74.125.95.132/search?q=cache:http://googlemapsbook.com/2007/01/22/extending-gmarker/ – Great Turtle Jun 29 '09 at 12:13

5 Answers5

25

For Google Maps JavaScript API v3, check out the examples here.

Source code available in normal and minimised forms.

Drew Noakes
  • 284,599
  • 158
  • 653
  • 723
  • 1
    Version 1.1.1 of MarkerWithLabel has been released with some fixes. – Drew Noakes Jan 22 '11 at 22:34
  • 1
    For an example of this API in use, check out a website I developed (keep zooming in on the map and you will see markers first, then labels as you zoom further): http://diveseven.com/atlas – Drew Noakes Jun 17 '11 at 16:11
  • Good one..But it is very slow as compared to regular Markers, when the number of markers in some Thousands.. Is there any alternate to this problem ? @Drew Noakes. – Pravin Kumar Nov 29 '12 at 10:55
  • 1
    @PravinKumar, I am not aware of any. I haven't hit any performance problems, but then I'm not trying to draw that many markers. I'm not sure how much I'd like to look at a map with thousands of dots on it, but of course it depends. When I've had that many data points, I've turned to clustering to convey the information to viewers, and only use markers when zoomed in enough to have a sensible number of them on screen. – Drew Noakes Dec 23 '12 at 13:46
  • MarkerWithLabel has an open issue #24 with the labels getting the wrong z-index. [jsfiddle example](http://jsfiddle.net/johntrepreneur/L2KYQ/2/) – johntrepreneur Aug 16 '13 at 19:20
  • @DrewNoakes - the link you provided drills down the 1.0.1 release directory and the latest release is 1.1.9 so you might want to change the link to point the [parent release directory](http://google-maps-utility-library-v3.googlecode.com/svn/tags/markerwithlabel/) to let the user pick the latest release. – johntrepreneur Aug 16 '13 at 19:28
  • @johntrepreneur - thanks for the tip. I've updated the answer. – Drew Noakes Aug 16 '13 at 23:58
  • 3
    The Examples link is dead. Here is the Google Maps JS V3 API reference page: https://developers.google.com/maps/documentation/javascript/reference#Marker – Lance Cleveland Nov 20 '16 at 01:25
13

There's a good label class here, though you'll have to add it alongside the markers.

Chris B
  • 15,144
  • 5
  • 32
  • 40
  • The ELabel class you pointed me to works pretty well. Amazing Google Maps site btw, don't know how I didn't find it in my original searches. Thanks! – Great Turtle Jun 30 '09 at 00:58
  • 4
    That's handy, though it doesn't work with v3 of the Google Maps JavaScript API. Also, if you zoom out far enough, then the labels overlap into an unreadable soup. Still, the best offering I've found so far. – Drew Noakes Oct 07 '10 at 21:17
  • Google Maps JS V3 API has a setLabel method for markers. https://developers.google.com/maps/documentation/javascript/reference#Marker – Lance Cleveland Nov 20 '16 at 01:24
7

If you just want to show label below the marker, then you can extend google maps Marker to add a setter method for label and you can define the label object by extending google maps overlayView like this..

Demo: jsFiddle

<script type="text/javascript">
    var point = { lat: 22.5667, lng: 88.3667 };
    var markerSize = { x: 22, y: 40 };


    google.maps.Marker.prototype.setLabel = function(label){
        this.label = new MarkerLabel({
          map: this.map,
          marker: this,
          text: label
        });
        this.label.bindTo('position', this, 'position');
    };

    var MarkerLabel = function(options) {
        this.setValues(options);
        this.span = document.createElement('span');
        this.span.className = 'map-marker-label';
    };

    MarkerLabel.prototype = $.extend(new google.maps.OverlayView(), {
        onAdd: function() {
            this.getPanes().overlayImage.appendChild(this.span);
            var self = this;
            this.listeners = [
            google.maps.event.addListener(this, 'position_changed', function() { self.draw();    })];
        },
        draw: function() {
            var text = String(this.get('text'));
            var position = this.getProjection().fromLatLngToDivPixel(this.get('position'));
            this.span.innerHTML = text;
            this.span.style.left = (position.x - (markerSize.x / 2)) - (text.length * 3) + 10 + 'px';
            this.span.style.top = (position.y - markerSize.y + 40) + 'px';
        }
    });
    function initialize(){
        var myLatLng = new google.maps.LatLng(point.lat, point.lng);
        var gmap = new google.maps.Map(document.getElementById('map_canvas'), {
            zoom: 5,
            center: myLatLng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        });
        var myMarker = new google.maps.Marker({
            map: gmap,
            position: myLatLng,
            label: 'Hello World!',
            draggable: true
        });
    }
</script>
<style>
    .map-marker-label{
        position: absolute;
    color: blue;
    font-size: 16px;
    font-weight: bold;
    }
</style>

This will work.

ndd
  • 2,921
  • 3
  • 23
  • 40
Ramyani
  • 933
  • 1
  • 7
  • 11
2

Here is some sample code which would solve the problem: https://github.com/printercu/google-maps-utility-library-v3-read-only/tree/master/markerwithlabel/examples

This is a custom class called InfoBox. You can see the API ref on the link above

The js file can be included from here: https://github.com/printercu/google-maps-utility-library-v3-read-only/blob/master/infobox/src/infobox.js

Cheers!

Himanshu Khurana
  • 605
  • 1
  • 6
  • 11
0

Even this question has been answered I have found this resource, which I think is a good solution for this issue and could be helpful. Also, a similar solution could be found here.

Community
  • 1
  • 1
AlexAndro
  • 1,896
  • 1
  • 27
  • 53