0

I have markers with labels like below:

Marker with white label

The code is similar to this answer:

var m = new google.maps.Marker({
  position: new google.maps.LatLng(lat, lng),
  label: {
    color: 'white',
    fontWeight: 'bold',
    text: 'Hello world',
  },
  icon: {
    labelOrigin: new google.maps.Point(11, 50),
    url: 'default_marker.png',
    size: new google.maps.Size(22, 40),
    origin: new google.maps.Point(0, 0),
    anchor: new google.maps.Point(11, 40),
  },
});

What I need in my project is to remove labels of certain markers. However, the official API docs seem to discuss only about adding the label. So, how to remove a label of a labeled marker?

Community
  • 1
  • 1
Akseli Palén
  • 27,244
  • 10
  • 65
  • 75

1 Answers1

2

After a little experimentation, the following seems to remove the label:

m.setLabel(null);

It is similar to how the markers are removed from the map:

m.setMap(null);

Update: one thing to keep in mind though, is that m.getLabel() returns undefined until the label is set with m.setLabel(...). After calling m.setLabel(null), a call to m.getLabel() will return null instead.

Akseli Palén
  • 27,244
  • 10
  • 65
  • 75
  • I think this version is still experimental so that may change with many other things. But still finally we'll have builtin label support. – Ergec Nov 09 '16 at 05:04