How can I create numbered map markers in Google Maps V3?

后端 未结 16 994
予麋鹿
予麋鹿 2020-11-28 18:13

I\'m working on a map that has multiple markers on it.

These markers use a custom icon, but I\'d also like to add numbers on top. I\'ve seen how this has been accomp

16条回答
  •  孤独总比滥情好
    2020-11-28 18:53

    This has now been added to the Mapping documentation and requires no third party code.

    You can combine these two samples:

    https://developers.google.com/maps/documentation/javascript/examples/marker-labels

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

    To get code like this:

    var labelIndex = 0;
    var labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789';
    
    function initialize() {
      var bangalore = { lat: 12.97, lng: 77.59 };
      var map = new google.maps.Map(document.getElementById('map-canvas'), {
        zoom: 12,
        center: bangalore
      });
    
      // This event listener calls addMarker() when the map is clicked.
      google.maps.event.addListener(map, 'click', function(event) {
        addMarker(event.latLng, map);
      });
    
      // Add a marker at the center of the map.
      addMarker(bangalore, map);
    }
    
    // Adds a marker to the map.
    function addMarker(location, map) {
      // Add the marker at the clicked location, and add the next-available label
      // from the array of alphabetical characters.
      var marker = new google.maps.Marker({
        position: location,
        label: labels[labelIndex],
        map: map,
        icon: 'image.png'
      });
    }
    
    google.maps.event.addDomListener(window, 'load', initialize);
    

    Note that if you have more than 35 markers, this method will not work as the label only shows the first character (using A-Z and 0-9 makes 35). Please vote for this Google Maps Issue to request that this restriction be lifted.

提交回复
热议问题