更改Leaflet点GeoJSON图层上的默认样式?


9

我需要在Leaflet地图中的点GeoJSON图层上更改样式。

我正在使用以下代码:

function onEachFeature(feature, layer) {
                      if (feature.properties && feature.properties.popupContent) {
                         layer.bindPopup(feature.properties.popupContent);
                      }
                     }

var myStyle = {
 "color": "#ff7800",
 "weight": 5,
 "opacity": 0.65
};

myGeoJSONLayer = L.geoJson(myGeoJSON, {
                      style: myStyle,
                      onEachFeature: onEachFeature,
             });

myGeoJSONLayer.addTo(map);                         

一切正常,但是我的地图上始终有标准的默认蓝色标记。

Answers:


15

要更改点标记,应使用该pointToLayer功能。请参阅示例页面

var geojsonMarkerOptions = {
    radius: 8,
    fillColor: "#ff7800",
    color: "#000",
    weight: 1,
    opacity: 1,
    fillOpacity: 0.8
};

L.geoJson(someGeojsonFeature, {
    pointToLayer: function (feature, latlng) {
        return L.circleMarker(latlng, geojsonMarkerOptions);
    }
}).addTo(map);
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.