Why don't the marker titles show in my Google Maps API3 application?

10,190

Solution 1

From what I understood, you want a tooltip visible at all times. There's the InfoBox Utility library that can create something like that. It's very flexible, which also means there are a lot of options to set. An annoyance, for example, is if the text gets too long it flows outside the box (so the width would need to be set dynamically).

Doc: http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobox/docs/examples.html Download: http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobox/src/

See the screenshot, if it's what you want, download infobox_packed.js and try the code below.

InfoBox example

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
      html, body, #map_canvas { margin: 0; padding: 0; height: 100% }
    </style>
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
    <script type="text/javascript" src="infobox_packed.js"></script>
    <script type="text/javascript">
      var map;
      var mapOptions = {
        center: new google.maps.LatLng(0.0, 0.0),
        zoom: 2,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      var count = 0;

      function initialize() {
        map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
        google.maps.event.addListener(map, 'click', function (event) {
          addMarker(event.latLng, "index #" + Math.pow(100, count));
          count += 1;
        });
      }

      function addMarker(pos, content) {
        var marker = new google.maps.Marker({
          map: map,
          position: pos
        });
        marker.setTitle(content);

        var labelText = content;

        var myOptions = {
          content: labelText
           ,boxStyle: {
              border: "1px solid black"
             ,background: "white"
             ,textAlign: "center"
             ,fontSize: "8pt"
             ,width: "86px"  // has to be set manually
             ,opacity: 1.0
            }
           ,disableAutoPan: true
           ,pixelOffset: new google.maps.Size(-43, -50) // set manually
           ,position: marker.getPosition()
           ,closeBoxURL: ""
           ,pane: "floatPane"
           ,enableEventPropagation: true
        };

        var ibLabel = new InfoBox(myOptions);
        ibLabel.open(map);
      }

      google.maps.event.addDomListener(window, 'load', initialize);
    </script>
  </head>
  <body>
    <div id="map_canvas"></div>
  </body>
</html>

Solution 2

This will not solve your problem but, why you are calling setTitle after creating a new Marker? Code like this:

var newMarker = new google.maps.Marker{
    position: location,
    map: map,
    title: MyMarkerTitle
}

When you call setTitle the API calls the MVCObject method set, so the code above is the same as:

var newMarker = new google.maps.Marker{
    position: location
};
newMarker.setMap(map);
newMarker.setTitle(myMarkerTitle);

this works too:

var newMarker = new google.maps.Marker{ position: location };
newMarker.set("map", map);
newMarker.set("title", myMarkerTitle);

and this:

var newMarker = new google.maps.Marker{ position: location };
newMarker.setValues({ map: map, title: myMarkerTitle });

Here's the documention.

Solution 3

AFAICT your code should work. It works for me. But the title only displays when you hover over the marker. It will not display until then. Your comments make it seem like you misunderstand how titles work. Their display is an onmouseover event. It's also possible that your title string is null or ''.

Share:
10,190
dangerChihuahua007
Author by

dangerChihuahua007

Updated on June 05, 2022

Comments

  • dangerChihuahua007
    dangerChihuahua007 almost 2 years

    I added markers onto my map via this function.

    markers: an array of Marker objects
    location: a LatLng object
    myMarkerTitle: the desired title for my marker

    // adds new markers to the map.
    function addAMarker(location, myMarkerTitle) {
        newMarker = new google.maps.Marker({
                position: location,
                map: map
        });
        newMarker.setTitle(myMarkerTitle);
        markers.push(newMarker);
    }
    

    It adds markers on the map, but the titles I assigned to the markers do not show. Why?

    In the end, I want a map with markers scattered about with titles above them. When a client hovers over and clicks on a marker, more details also surface. Thank you.

  • dangerChihuahua007
    dangerChihuahua007 about 12 years
    Thank you! This tool is straightforward and meets my needs.