Starting in October 2016, the official API provides the ability to add constantly visible labels with a length of more than one letter. See this answer from a Google contributor .
var m = new google.maps.Marker({ position: new google.maps.LatLng(lat, lng), label: 'Hello world', });
By default, the result looks like this:
Pretty unreadable. Fortunately, the API also allows MarkerLabel instead of a simple line:
var m = new google.maps.Marker({ position: new google.maps.LatLng(lat, lng), label: { color: 'white', fontWeight: 'bold', text: 'Hello world', }, });
The snippet above gives the following result:
However, the original question asked if the label could be located below the marker. MarkerLabel notes mention that this is possible with the custom icon and the labelOrigin
property. If we want to use the default icon, then
Pretty good! However, this whole approach has a drawback that does not have in the field of the original question: readability with each type of card. If the map type is changed from satellite to the default value, the resulting label is difficult to read:
An easy but not ideal way to avoid low contrast in both types is to set color: 'gray'
:
However, the gray color fails near urban areas. A better option would be to use the CSS text-shadow
property to draw black linings for white text. However, I cannot find a way to apply the property to labels, because several DOM elements created by Google Maps define the class:
The best option I came up with is to detect changes in map type and label color for each marker:
map.addListener('maptypeid_changed', function () { var typeToColor, type, color, k, label; typeToColor = { 'terrain': 'black', 'roadmap': 'black', 'hybrid': 'white', 'satellite': 'white', }; type = map.getMapTypeId(); color = typeToColor[type]; for (k in markers) { if (markers.hasOwnProperty(k)) { label = markers[k].getLabel(); label.color = color; markers[k].setLabel(label); } } });
However, even this would not work on snowy or cloudy satellite imagery. I think he is still good enough in most cases. However, it’s nice to be able to display visible shortcuts with the official API without any plugins :)