Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

Leaflet PointToLayer Style on AJAX Loaded GeoJson Data

I'm having a bit of difficulty fixing the default point style on my Leaflet map, currently the points appear in the correct positions after being loaded via ajax.

But the current code does not alter the style and I am left with the default light-blue circle styling of the points:

function addressStyle(feature) {
        return {
            color: '#000',
            fillColor: '#199900',
            fillOpacity: 0.5,
            radius: 100,
        };
    }


    var address_points = new L.geoJson('', {
        pointToLayer: function (feature, latlng) {
            return L.circleMarker(latlng, addressStyle);
        },
        onEachFeature: function (feature, layer) {
            layer.bindPopup('<h1>'+feature.properties.address+'</h1>');
        },
    });
    address_points.addTo(map);

    $.ajax({
        dataType: "json",
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        url:  "{{url('/addresses/geojson')}}",
        success: function(data) {
            $(data.features).each(function(key, data) {
                address_points.addData(data);
            });
        },
        error: function(data){

        }
    });

Is there a better way to handle this? I am just getting into AJAX loading of related GeoJSON data but am stuck on how to proceed with styling of this features when I am having no difficulty with my other layers that aren't points.

question from:https://stackoverflow.com/questions/65910189/leaflet-pointtolayer-style-on-ajax-loaded-geojson-data

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want to apply the style to all layers (except markers) then change your code to:

var address_points = new L.geoJson('', {
        style: addressStyle,
        pointToLayer: function (feature, latlng) {
            return L.circleMarker(latlng);
        },
        onEachFeature: function (feature, layer) {
            layer.bindPopup('<h1>'+feature.properties.address+'</h1>');
        },
    });

When you only want to apply the style to the CircleMarker you need to pass the style as object and not as function.

You can simply call the addressStyle function:

var address_points = new L.geoJson('', {
        pointToLayer: function (feature, latlng) {
            return L.circleMarker(latlng, addressStyle());
        },
        onEachFeature: function (feature, layer) {
            layer.bindPopup('<h1>'+feature.properties.address+'</h1>');
        },
    });

or define addessStyle as object:

var addressStyle = {
            color: '#000',
            fillColor: '#199900',
            fillOpacity: 0.5,
            radius: 100,
        };

var address_points = new L.geoJson('', {
        pointToLayer: function (feature, latlng) {
            return L.circleMarker(latlng, addressStyle);
        },
        onEachFeature: function (feature, layer) {
            layer.bindPopup('<h1>'+feature.properties.address+'</h1>');
        },
    });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...