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

后端 未结 16 910
予麋鹿
予麋鹿 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:55

    Unfortunately it's not very easy. You could create your own custom marker based on the OverlayView class (an example) and put your own HTML in it, including a counter. This will leave you with a very basic marker, that you can't drag around or add shadows easily, but it is very customisable.

    Alternatively, you could add a label to the default marker. This will be less customisable but should work. It also keeps all the useful things the standard marker does.

    You can read more about the overlays in Google's article Fun with MVC Objects.

    Edit: if you don't want to create a JavaScript class, you could use Google's Chart API. For example:

    Numbered marker:

    http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=7|FF0000|000000
    

    Text marker:

    http://chart.apis.google.com/chart?chst=d_map_spin&chld=1|0|FF0000|12|_|foo
    

    This is the quick and easy route, but it's less customisable, and requires a new image to be downloaded by the client for each marker.

    0 讨论(0)
  • 2020-11-28 18:57

    Perhaps there are those still looking for this but finding Google Dynamic icons deprecated and other map-icon libraries just a little bit too ugly.

    To add a simple marker with any number inside using a URL. In Google Drive using the Google My Maps, it creates numbered icons when using a map layer that is set to 'Sequence of Numbers' and then adding markers/points on the map.

    Looking at the source code, Google has their own way of doing it through a URL:

    https://mt.google.com/vt/icon/name=icons/onion/SHARED-mymaps-container-bg_4x.png,icons/onion/SHARED-mymaps-container_4x.png,icons/onion/1738-blank-sequence_4x.png&highlight=ff000000,0288D1,ff000000&scale=2.0&color=ffffffff&psize=15&text=56&font=fonts/Roboto-Medium.ttf
    

    Above URL

    I haven't played extensively with it but by changing the hex color codes in the 'highlight' parameter(color parameter does not change the color as you may think), the 'text' value can be set to any string and you can make a nice round icon with any number/value inside. I'm sure the other parameters may be of use too.

    One caveat with this approach, who knows when Google will remove this URL from the world!

    0 讨论(0)
  • 2020-11-28 18:58

    You may want to download a set of numbered icons from the sources provided at this site:

    • Google Marker Icons

    Then you should be able to do the following:

    <!DOCTYPE html>
    <html> 
    <head> 
        <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
        <title>Google Maps Demo</title> 
        <script type="text/javascript"
                src="http://maps.google.com/maps/api/js?sensor=false"></script> 
    
        <script type="text/javascript"> 
        function initialize() {
    
          var myOptions = {
            zoom: 11,
            center: new google.maps.LatLng(-33.9, 151.2),
            mapTypeId: google.maps.MapTypeId.ROADMAP
          }
    
          var map = new google.maps.Map(document.getElementById("map"), myOptions);
    
          var locations = [
            ['Bondi Beach', -33.890542, 151.274856, 4],
            ['Coogee Beach', -33.923036, 151.259052, 5],
            ['Cronulla Beach', -34.028249, 151.157507, 3],
            ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
            ['Maroubra Beach', -33.950198, 151.259302, 1]
          ];
    
          for (var i = 0; i < locations.length; i++) {
              var image = new google.maps.MarkerImage('marker' + i + '.png',
                          new google.maps.Size(20, 34),
                          new google.maps.Point(0, 0),
                          new google.maps.Point(10, 34));
    
              var location = locations[i];
              var myLatLng = new google.maps.LatLng(location[1], location[2]);
              var marker = new google.maps.Marker({
                  position: myLatLng,
                  map: map,
                  icon: image,
                  title: location[0],
                  zIndex: location[3]
              });
          }
        }
        </script> 
    </head> 
    <body style="margin:0px; padding:0px;" onload="initialize();"> 
        <div id="map" style="width:400px; height:500px;"></div> 
    </body> 
    </html>
    

    Screenshot from the above example:

    Google Numbered Marker Icons

    Note that you can easily add a shadow behind the markers. You may want to check the example at the Google Maps API Reference: Complex Markers for more info about this.

    0 讨论(0)
  • 2020-11-28 18:59

    Based on @dave1010 answer but with updated https links.

    Numbered marker:

    https://chart.googleapis.com/chart?chst=d_map_pin_letter&chld=7|FF0000|000000
    

    Text marker:

    https://chart.googleapis.com/chart?chst=d_map_spin&chld=1|0|FF0000|12|_|Marker
    
    0 讨论(0)
提交回复
热议问题