gpt4 book ai didi

javascript - 如何在传单中加载 SVG 图标?

转载 作者:行者123 更新时间:2023-11-30 19:18:14 26 4
gpt4 key购买 nike

我必须在传单 map 中加载标记 SVG。我有返回的方法:

fillColor: ""
fillOpacity: 1
path: ""
scale:
strokeColor: ""
strokeWeight:

如何包含到 html 代码中,我在传单中阅读了加载 SVG 的方法:

const url: any = (this.getMarkerIcon(marker))
const path: string = url.path

const fillColor = url.fillColor
const strokeColor = url.strokeColor
const fillOpacity = url.fillOpacity
const iconSettings = {
mapIconUrl: '<svg version="1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 149 178"><path fill="{fillColor}" stroke="{strokeColor} d="{path}"/></svg>',
};
let divIcon = L.divIcon({

className: "leaflet-data-marker",
html: L.Util.template(iconSettings.mapIconUrl, iconSettings).replace('#', '%23'),
iconAnchor: [12, 32],
iconSize: [25, 30],
popupAnchor: [0, -28]
});

如何在传单中加载 SVG 图标?

最佳答案

对于 divicon有一个选项html,所以你需要传入html参数的SVG图标。

代码片段:

var map = L.map('map', {
zoom: 5,
//minZoom:9,
center: new L.latLng([50, 12]),
layers: L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'),
layers: [
L.tileLayer('https://cartodb-basemaps-{s}.global.ssl.fastly.net/light_nolabels/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> &copy; <a href="http://cartodb.com/attributions">CartoDB</a>',
subdomains: 'abcd',
maxZoom: 19
})
]
}),
locationLayer = new L.FeatureGroup(),
markerTemp = L.marker(),
iconSettings = {
mapIconUrl: '<svg version="1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 149 178"><path fill="{mapIconColor}" stroke="#FFF" stroke-width="6" stroke-miterlimit="10" d="M126 23l-6-6A69 69 0 0 0 74 1a69 69 0 0 0-51 22A70 70 0 0 0 1 74c0 21 7 38 22 52l43 47c6 6 11 6 16 0l48-51c12-13 18-29 18-48 0-20-8-37-22-51z"/><circle fill="{mapIconColorInnerCircle}" cx="74" cy="75" r="61"/><circle fill="#FFF" cx="74" cy="75" r="{pinInnerCircleRadius}"/></svg>',
mapIconColor: '#cc756b',
mapIconColorInnerCircle: '#fff',
pinInnerCircleRadius: 48
},

// icon normal state
divIcon = L.divIcon({
className: "leaflet-data-marker",
html: L.Util.template(iconSettings.mapIconUrl, iconSettings),
iconAnchor: [12, 32],
iconSize: [25, 30],
popupAnchor: [0, -28]
}),

// icon active state
divIconActive = L.divIcon({
className: "leaflet-data-marker",
html: L.Util.template(iconSettings.mapIconUrl, iconSettings),
iconAnchor: [18, 42],
iconSize: [36, 42],
popupAnchor: [0, -30]
}),

coords = [
[53, 13],
[49, 10],
[46, 12],
[51, 16]
],
markerArray = [],
iMarker = -1;

function setActiveIcon(marker) {
marker.setIcon(divIconActive);
};

coords.forEach((e, i) => {

var marker = L.marker(e, {
icon: divIcon,
id: i
});

locationLayer.addLayer(marker);

marker.on('mouseover', function(e) {
if (iMarker == i) return;
setTimeout(setActiveIcon, 10, this);
if (iMarker >= 0) markerArray[iMarker].setIcon(divIcon);
iMarker = i;
});

marker.on('mouseout', function(e) {
this.setIcon(divIcon);
iMarker = -1;
});
markerArray[i] = marker;
});

locationLayer.addTo(map);
html,
body,
#map {
height: 100%;
width: 100%;
background-color: #d5dbdd;
}
#map {
box-shadow: (0px 0px 20px rgba(0, 0, 0, .3))
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.5.1/leaflet.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.5.1/leaflet.css" rel="stylesheet" />

<div id="map"></div>

关于javascript - 如何在传单中加载 SVG 图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57767359/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com