admin管理员组

文章数量:1290226

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.

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.

Share Improve this question edited Mar 18, 2012 at 23:20 dangerChihuahua007 asked Mar 18, 2012 at 21:59 dangerChihuahua007dangerChihuahua007 20.9k38 gold badges128 silver badges211 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

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./svn/trunk/infobox/docs/examples.html Download: http://google-maps-utility-library-v3.googlecode./svn/trunk/infobox/src/

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

<!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./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>

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.

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 ments 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 ''.

本文标签: javascriptWhy don39t the marker titles show in my Google Maps API3 applicationStack Overflow