Creating a multi color svg icon for google map marker

后端 未结 1 1988
一生所求
一生所求 2020-12-11 10:51

I want to add a svg icon to the google map marker. Right now i am able to create a svg icon by a single path, Please find the code below:

    var car = \"M         


        
相关标签:
1条回答
  • 2020-12-11 11:18

    Per this link, you can use a data:image URL containing SVG for the icon of a marker.

    var $markerImage = document.querySelector('.markerImage'),
      markerImageSvg = $markerImage.innerHTML || '';
    var marker = new google.maps.Marker({
        position: map.getCenter(),
        map: map,
        clickable: false,
        icon: {
          anchor: new google.maps.Point(16, 16),
          url: 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(markerImageSvg.replace('{{background}}', colors[0]))
        }
      });
    

    proof of concept fiddle

    code snippet:

    function initialize() {
      var map = new google.maps.Map(
        document.getElementById("map_canvas"), {
          center: new google.maps.LatLng(37.4419, -122.1419),
          zoom: 13,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        });
    
      var $markerImage = document.querySelector('.markerImage'),
        markerImageSvg = $markerImage.innerHTML || '';
      new google.maps.Marker({
        position: map.getCenter(),
        map: map,
        clickable: false,
        icon: {
          anchor: new google.maps.Point(16, 16),
          url: 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(markerImageSvg.replace('{{background}}', "#FF4847"))
        }
      });
    }
    google.maps.event.addDomListener(window, "load", initialize);
    html,
    body,
    #map_canvas {
      height: 100%;
      width: 100%;
      margin: 0px;
      padding: 0px
    }
    .markerImage {
      display: none;
    }
    <script src="https://maps.googleapis.com/maps/api/js"></script>
    <div id="map_canvas"></div>
    <div class="markerImage">
      <svg width="32" height="32" viewBox="0 0 32 32" xmlns="http://www.w3.org/2000/svg">
        <defs>
          <linearGradient id="myGradient" gradientUnits="userSpaceOnUse" x1="5" y1="16" x2="27" y2="16" gradientTransform="rotate(90 16 16)">
            <stop offset="0" stop-color="white" />
            <stop offset="1" stop-color="black" />
          </linearGradient>
          <filter id="dropshadow">
            <feGaussianBlur in="SourceAlpha" stdDeviation="1" />
            <feOffset dx="0" dy="0" result="offsetblur" />
            <feFlood flood-color="#000" flood-opacity=".25" />
            <feComposite in2="offsetblur" operator="in" />
            <feMerge>
              <feMergeNode/>
              <feMergeNode in="SourceGraphic" />
            </feMerge>
          </filter>
        </defs>
        <path fill="{{background}}" stroke="white" stroke-width="1.5" style="filter:url(#dropshadow)" d="M3.5 3.5h25v25h-25z" />
        <path opacity=".15" fill="url(#myGradient)" d="M5 5h22v22H5z" />
        <path fill="#88D063" d="m12.18182,8.90909v2.72727c0,1.8 1.5,2.78182 3.27273,2.72727v7.09091c-0.88691,-1.77273 -2.72727,-3.81818 -5.45455,-2.72727c0,0 1.63636,4.36364 6,4.36364s6,-4.36364 6,-4.36364c-2.72727,-1.09091 -4.56873,0.95455 -5.45454,2.72727v-7.09091c1.77272,0.05455 3.27273,-0.92727 3.27273,-2.72727v-2.72727l-2.18182,2.18182l-1.63636,-2.18182l-1.63636,2.18182l-2.18182,-2.18182z"
        />
      </svg>
    </div>

    0 讨论(0)
提交回复
热议问题