我正在使用 Leaflet 显示 Geojson 图层。我想更改默认标记图标大小。
这是我想出的代码:
$.ajax({
type: "GET",
url: "data.json",
dataType: 'json',
success: function (response) {
geojsonLayer = L.geoJson(response, {
pointToLayer: function(featuer, latlng) {
var smallIcon = L.Icon.extend({
options: {
'iconSize': [10, 10]
}
});
var myIcon = new smallIcon();
return L.marker(latlng, {icon: smallIcon});
},
onEachFeature: onEachFeature
}).addTo(map);
}
});
但是,我在加载页面时遇到 JavaScript 错误:
Uncaught TypeError: Cannot read property 'popupAnchor' of undefined
有什么线索吗?
最佳答案
我认为当您扩展 L.Icon 类时,您必须指定 IconUrl,它是指向用于图标的图像文件的链接。请参阅文档:http://leafletjs.com/reference.html#icon
由于smallIcon构造函数不满足所有必需的选项,myIcon将是未定义的。
试试这个:
var myIcon = L.icon({
iconUrl: 'leaflet/images/marker-icon.png',
iconSize: [10,10],
iconAnchor: [22, 94],
popupAnchor: [-3, -76],
shadowUrl: 'leaflet/images/marker-shadow.png',
shadowSize: [68, 95],
shadowAnchor: [22, 94]
});
L.marker(latlng, {icon: myIcon}).addTo(map);
关于javascript - Leaflet:geojson 层上的标记图标大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16168188/