[英]change leaflet marker icon
我正在使用 Dennis Wilhelm 的 Leaflet Slider 来显示 Leaflet Z1D78DC8ED512144014 上的数据变化
我正在尝试更改更改标记图标但不正确。 那么,当使用 Leaflet Slider 以显示随时间的变化时,如何更改标记图标? 我必须对原始 SliderControl.js 进行哪些更改?
提前致谢!
以下是 Dennis Wilhelm 的 Leaflet Slider 代码的链接:
https://github.com/dwilhelm89/LeafletSlider/blob/master/SliderControl.js
您可以创建新的图标类,如下所示:
var LeafIcon = L.Icon.extend({
options: {
iconSize: [38, 95],
shadowSize: [50, 64],
iconAnchor: [22, 94],
shadowAnchor: [4, 62],
popupAnchor: [-3, -76]
}
});
然后创建一个新的图标对象,如下所示:
var greenIcon = new LeafIcon({
iconUrl: 'http://leafletjs.com/examples/custom-icons/leaf-green.png',
shadowUrl: 'http://leafletjs.com/examples/custom-icons/leaf-shadow.png'
})
现在您可以在地图中标记的图标上方,如下所示:
L.marker([51.5, -0.09], {icon: greenIcon}).addTo(map);
您可以参考此文档以获取更多信息。
对于滑块控件,您需要创建两个图像:
(1) Marker Icon [ Use name: marker-icon.png ]
(2) Marker Icon Shadow [ Use name: marker-shadow.png ]
之后,您可以指定默认图像路径,如下所示:
L.Icon.Default.imagePath = "Url to the image folder"; // This specifies image path for marker icon.
e.x L.Icon.Default.imagePath = "http://leafletjs.com/examples/custom-icons";
所以图标 URL 将是:
Icon URL : http://leafletjs.com/examples/custom-icons/marker-icon.png
Shadow URL: http://leafletjs.com/examples/custom-icons/marker-shadow.png
这将是原始slidercontrol.js 文件中的确切更改
if (this._layer) {
var index_temp = 0;
this._layer.eachLayer(function (layer) {
var greenIcon = L.icon({ //add this new icon
iconUrl: i+'.png',
shadowUrl: 'leaf-shadow.png',
iconSize: [38, 95], // size of the icon
shadowSize: [50, 64], // size of the shadow
iconAnchor: [22, 94], // point of the icon which will correspond to marker's location
shadowAnchor: [4, 62], // the same for the shadow
popupAnchor: [-3, -76] // point from which the popup should open relative to the iconAnchor
});
options.markers[index_temp] = layer;
options.markers[index_temp].setIcon(greenIcon); //Here set the icon to indiviual markers
++index_temp;
});
options.maxValue = index_temp - 1;
this.options = options;
}
var century21icon = L.icon({ iconUrl: 'https://i.ibb.co/sJrMTdz/favicon-32x32.png', iconSize: [20, 20] }); var maker = L.marker([25.045403,121.526088],{icon: century21icon}).addTo(map);
如果有人想知道如何使用 vue2-leaflet 在 Typescript 中做到这一点,下面是一个示例代码。 您甚至可以在 l-marker 上使用 v-for 一次性绘制多个标记:
// inside template
<l-marker :lat-lng="position"
:icon="defaultIcon" :key="index">
<l-icon
:icon-size=[20,40]
:popupAnchor= [0,0]
:iconUrl= "'assets/logo.png'"
:shadowUrl= "'assets/logo.png'" >
<v-icon medium >
mdi-target
</v-icon>
</l-icon>
</l-marker>
// default icon in script
defaultIcon = L.icon({
iconUrl: "https://unpkg.com/leaflet@1.0.3/dist/images/marker-icon.png",
shadowUrl: require('../static/marker.png'),
iconSize: [20, 30],
iconAnchor: [18, 18],
popupAnchor: [0, -10],
shadowSize: [0, 0],
shadowAnchor: [10, 10]
});
我们可以简单地使用一个 Unicode 字符作为图标,并传入一个 CSS class 名称来进一步改进它。 在浏览器之间呈现不同的图标很少。
L.marker([45, 5], {icon: L.divIcon({className: 'poi', html: '<b>🚰</b>'})}).addTo(map);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.