Google maps Marker Label with multiple characters

前端 未结 7 1282
别那么骄傲
别那么骄傲 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 15:02

    OK, here is one solution I have come up with which is pretty messed up.

    I put the full label text into the div using the fontFamily label attribute. Then I use querySelectorAll to match the resulting style attributes to pull out the refs and rewrite the tags once the map has loaded:

    var label = "A123";
    var marker = new google.maps.Marker({
      position: latLon,
      label: {
        text: label,
        // Add in the custom label here
        fontFamily: 'Roboto, Arial, sans-serif, custom-label-' + label
      },
      map: map,
      icon: {
        path: 'custom icon path',
        fillColor: '#000000',
        labelOrigin: new google.maps.Point(26.5, 20),
        anchor: new google.maps.Point(26.5, 43), 
        scale: 1
      }
    });
    
    google.maps.event.addListener(map, 'idle', function() {
      var labels = document.querySelectorAll("[style*='custom-label']")
      for (var i = 0; i < labels.length; i++) {
        // Retrieve the custom labels and rewrite the tag content
        var matches = labels[i].getAttribute('style').match(/custom-label-(A\d\d\d)/);
        labels[i].innerHTML = matches[1];
      }
    });
    

    This seems pretty brittle. Are there any approaches which are less awful?

提交回复
热议问题