Google maps Marker Label with multiple characters

前端 未结 7 1280
别那么骄傲
别那么骄傲 2020-11-27 14:48

I am trying to add a 4 character label (eg \'A123\') to a Google Maps marker which has a wide icon defined with a custom path.



        
7条回答
  •  感情败类
    2020-11-27 14:59

    You can use MarkerWithLabel with SVG icons.

    Update: The Google Maps Javascript API v3 now natively supports multiple characters in the MarkerLabel

    proof of concept fiddle (you didn't provide your icon, so I made one up)

    Note: there is an issue with labels on overlapping markers that is addressed by this fix, credit to robd who brought it up in the comments.

    code snippet:

    function initMap() {
      var latLng = new google.maps.LatLng(49.47805, -123.84716);
      var homeLatLng = new google.maps.LatLng(49.47805, -123.84716);
    
      var map = new google.maps.Map(document.getElementById('map_canvas'), {
        zoom: 12,
        center: latLng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      });
    
      var marker = new MarkerWithLabel({
        position: homeLatLng,
        map: map,
        draggable: true,
        raiseOnDrag: true,
        labelContent: "ABCD",
        labelAnchor: new google.maps.Point(15, 65),
        labelClass: "labels", // the CSS class for the label
        labelInBackground: false,
        icon: pinSymbol('red')
      });
    
      var iw = new google.maps.InfoWindow({
        content: "Home For Sale"
      });
      google.maps.event.addListener(marker, "click", function(e) {
        iw.open(map, this);
      });
    }
    
    function pinSymbol(color) {
      return {
        path: 'M 0,0 C -2,-20 -10,-22 -10,-30 A 10,10 0 1,1 10,-30 C 10,-22 2,-20 0,0 z',
        fillColor: color,
        fillOpacity: 1,
        strokeColor: '#000',
        strokeWeight: 2,
        scale: 2
      };
    }
    google.maps.event.addDomListener(window, 'load', initMap);
    html,
    body,
    #map_canvas {
      height: 500px;
      width: 500px;
      margin: 0px;
      padding: 0px
    }
    .labels {
      color: white;
      background-color: red;
      font-family: "Lucida Grande", "Arial", sans-serif;
      font-size: 10px;
      text-align: center;
      width: 30px;
      white-space: nowrap;
    }
    
    
    

提交回复
热议问题