Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add text above a marker on Google Maps in JS?

I found exactly the same question for Java, but I'd like to do that in JS. So, how to add text above a marker on Google Maps in JS?

like image 970
Karol Selak Avatar asked Sep 11 '17 15:09

Karol Selak


2 Answers

As stated in my comment, You can use multiple letters for the map marker label.

If you use the label property when instantiating a new google.maps.Marker class, you pass it an object, with one of the properties text.

var marker = new google.maps.Marker({
    position: new google.maps.LatLng(0, 0),
    icon: 'https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png',
    label: { color: '#00aaff', fontWeight: 'bold', fontSize: '14px', text: 'Your text here' }
});

Check the fiddle here.

like image 113
G.Hunt Avatar answered Sep 18 '22 16:09

G.Hunt


I think what you're looking for isn't included in the standard library. Google do provide this interesting utility library though so you can make your own markers, labels. The one you want is the MarkerWithLable class. From the link:

This class behaves like google.maps.Marker, but it supports the association of a label with the marker. If the marker is draggable, so too will be the label. In addition, a marker with a label responds to all mouse events in the same manner as a regular marker. It also fires mouse events and "property changed" events just as a regular marker would.

Looks like it's used in much the same way a the standard Marker class and there appears to be ample examples of it's use kicking around so good luck and I hope that this was helpful :)

like image 23
DMcCallum83 Avatar answered Sep 18 '22 16:09

DMcCallum83