0

I want to display data from my database which I get in JSON format with getJSON function. I want to display these data on all GeoJSON features based on a condition. I am trying to do that with the onEachFeature function which is called as a property of the GeoJSON. How will I fit a jQuery funtion in a JS function? Because I compare variables that I need from both functions. Currently I have no luck managing this.

This is the JavaScript code:

var rbu_layer
$.getJSON("getdata.php", function(data) {
        var length = data.station_code.length;
        var station_code = [];
        var localid = feature.properties.localId;
        for (var i = 0; i < length; i++) {
            station_code[i] = data.station_code[i];
            if (station_code[i]==localid) {
                var popupContent = localid + "</br>" + station_code[i];
            }
        }
});
function onEachFeature(feature, layer) {

    if (feature.properties && feature.properties.popupContent) {
        layer.bindPopup(feature.properties.popupContent);
    }
    layer.bindPopup(popupContent);
}
var map = L.map('map').setView([51.505, -0.09], 3);

rbu_layer = L.geoJSON(rbd, {
    onEachFeature: onEachFeature
}).addTo(map);
map.fitBounds(rbu_layer.getBounds());

EDIT: I managed to do a workaround but still havent succeeded .Iam closer though

var map = L.map('map').setView([51.505, -0.09], 3);
var rbu_layer

function addData(data,map) {
    rbu_layer = L.geoJSON(rbd, {
        onEachFeature: function(feature, layer)  {
            var length = data.station_code.length;
            var station_code = [];
            var localid = feature.properties.localId;
            for (var i = 0; i < length; i++) {
                station_code[i] = data.station_code[i];
                if (station_code[i]==localid) {
                    var popupContent = localid + "</br>" + station_code[i] + "Hello";
                }
            }
            if (feature.properties && feature.properties.popupContent) {
                layer.bindPopup(feature.properties.popupContent);
            }
            layer.bindPopup(popupContent);
        }
    }).addTo(map);
    map.fitBounds(rbu_layer.getBounds());
}

$.getJSON("getdata.php", function(data) {
    addData(data,map);
});
  • see jsfiddle.net/zafrii/6s77xfhg – Mapperz May 18 at 17:51
  • i want first to compare feature property with php data to make sure they match, then display the php data not the property.Also my geojson file is on the same folder ,separate file – Peslis May 18 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.