stackOverFlew stackOverFlew - 3 months ago 33
Javascript Question

javascript google maps API - change title of marker

I am trying to populate the small annotation on a google map marker icon. Title doesn't seem to do it. I read the documentation, but can't find anything.

enter image description herehttps://developers.google.com/maps/documentation/javascript/examples/marker-labels

var marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i]["lat"], locations[i]["lng"]),
map: map,
title: "a title here"


});


Right now the annotations are blank, even though the title for the annotations is set. I never figured out what the title is actually used for either, since label is for the character inside the icon and the title has no effect on the annotation.

What is the property of the marker with which you can populate the annotation with text?

EDIT:
Thanks Samuel Toh.

Here is the the working code I ended up using:

var locations;// use this as the array which holds the data
for (i = 0; i < locations.length; i++) {
about the locations you are adding to the map
var marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i]["lat"], locations[i]["lng"]),
map: map,
title: activities[i]["city"]

});


//extend the bounds to include each markers position
bounds.extend(marker.position);

google.maps.event.addListener(marker, "click", (function(marker, i) {
return function() {
infowindow.setContent(locations[i]["city"]);
infowindow.open(map, marker);
}
})(marker, i));
}

Answer

You need an infoWindow object to display the title text.

See: https://developers.google.com/maps/documentation/javascript/examples/infowindow-simple

var infowindow = new google.maps.InfoWindow({
    content: "<span>any html goes here</span>" });

var marker = new google.maps.Marker({
    position: myLatlng,
    map: map,
    title: 'Uluru (Ayers Rock)' });

google.maps.event.addListener(marker, 'click', function() {   infowindow.open(map,marker); });