How do I use color code in inline svgs (google maps)

前端 未结 1 1375
死守一世寂寞
死守一世寂寞 2021-01-29 03:33

I am displaying a custom marker as .svg in google maps API in JavaScript.

It seems like that the google maps api does not like using #00492C in code.

相关标签:
1条回答
  • 2021-01-29 04:13

    You need to escape the # character as that's reserved for the start of a fragment identifier in a URL. Replace all occurances of # with %23

    icon: {
        url: 'data:image/svg+xml;utf-8, \
              <svg width="30" height="48" viewBox="1 -10 60 78" xmlns="http://www.w3.org/2000/svg"> \
              <path fill="%2300492C" d="M53.1,48.1c3.9-5.1,6.3-11.3,6.3-18.2C59.4,13.7,46.2,0.5,30,0.5C13.8,0.5,0.6,13.7,0.6,29.9 c0,6.9,2.5,13.1,6.3,18.2C12.8,55.8,30,77.5,30,77.5S47.2,55.8,53.1,48.1z"></path> \
              </svg>'
    }
    

    proof of concept fiddle

    code snippet:

    function initMap() {
      var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 4,
        center: {
          lat: -33,
          lng: 151
        }
      });
      var beachMarker = new google.maps.Marker({
        position: {
          lat: -33.890,
          lng: 151.274
        },
        map: map,
        icon: {
          url: 'data:image/svg+xml;utf-8, \
                  <svg width="30" height="48" viewBox="1 -10 60 78" xmlns="http://www.w3.org/2000/svg"> \
                  <path fill="%2300492C" d="M53.1,48.1c3.9-5.1,6.3-11.3,6.3-18.2C59.4,13.7,46.2,0.5,30,0.5C13.8,0.5,0.6,13.7,0.6,29.9 c0,6.9,2.5,13.1,6.3,18.2C12.8,55.8,30,77.5,30,77.5S47.2,55.8,53.1,48.1z"></path> \
                  </svg>'
        }
      });
    }
    html,
    body,
    #map {
      height: 100%;
      margin: 0;
      padding: 0;
    }
    <div id="map"></div>
    <!-- Replace the value of the key parameter with your own API key. -->
    <script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap">
    </script>

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