问题
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. fill="#0000"
does not work, only fill="green"
would work.
var 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="#00492C" 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>'
}
I am expecting my marker getting dark green but instead it just does not appear anymore.
回答1:
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>
来源:https://stackoverflow.com/questions/57426359/how-do-i-use-color-code-in-inline-svgs-google-maps