3

I have a map with 4 layers with the same attributes (not even the same values). an example of geoJSON is below:

{
"type": "FeatureCollection",

"features": [
{ "type": "Feature", "id": 0, "properties": { "CODIF": "PBA177432V1", "Position_X": 397632.884, "Position_Y": 419958.3456, "Point_Kilo": 177000, "Type": "2CmPt", "Voie": "V1", "dist_rail": 2.75, "Status": "PBA 3.50m" }, "geometry": { "type": "Point", "coordinates": [ -6.511578939607725, 34.374296518950914 ] } }
,
{ "type": "Feature", "id": 1, "properties": { "CODIF": "PBA177437V1", "Position_X": 397633.7158, "Position_Y": 419952.8751, "Point_Kilo": 177000, "Type": "STM.TT (PA)", "Voie": "V1", "dist_rail": 2.75, "Status": "PBA 3.50m" }, "geometry": { "type": "Point", "coordinates": [ -6.51156926129586, 34.37424727692602 ] } }
,
{ "type": "Feature", "id": 2, "properties": { "CODIF": "PBA177457V1", "Position_X": 397636.6028, "Position_Y": 419933.597, "Point_Kilo": 177000, "Type": "STM.TT (PA)", "Voie": "V1", "dist_rail": 2.75, "Status": "PBA 3.50m" }, "geometry": { "type": "Point", "coordinates": [ -6.511535636308449, 34.374073743241354 ] } } 
]
}

I want to add a function that searches by value of property. ("CODIF" in this case). It means that when a user enters a value of property "CODIF" of a feature, he sees this feature. I've added a function that searches by longitude and latitude values, but it has no relation with geoJSON properties (even if there are longitude and latitude properties in geoJSON file), as shown below:

$('#envoyer').click(function() {
    var lat = latitude.val();
    var long = longitude.val();

    if(long != '' && lat != '') {
        vectorSource.clear();
        vectorSource.addFeature(
            new ol.Feature({
                geometry: new ol.geom.Point([parseFloat(long), parseFloat(lat)]).transform('EPSG:4326', 'EPSG:3857')
            })
        );

        wkt.val('POINT(' + long + ' ' + lat + ')');
        map.getView().fit(vectorSource.getExtent(), map.getSize());
    }
    return false;
});

It responds to my need without being related to geoJSON properties values, and it's because of the nature of longitude and latitude properties, but for searching by "CODIF" property values, I must relate the function to geoJSON properties.

The HTML code of the function i should add is:

<div id="search">
  <input type="text" placeholder="Search PKs by CODIF">
  <button type="button">Search</button>
</div>

I started my function with something like:

$(document).on('click', '#search button', function() {
  var value = $('#search input').val();

What i need is to do a comparison of what a user entered as a value, with the values of "CODIF" property of geoJSON file, if it's found, the user will see the feature of this CODIF's value, and if not, he reads a message like: Not found.

  • Someone to help me please so i can continue my project. – drissrais Sep 26 '16 at 10:57
1

In order to search for a feature based on a properties you can loop through your vector source and verify each feature's property "CODEIF" with the one desired like this:

yourVectorSource.forEachFeature( function(feature){
    if (feature.get('CODEIF')==yourDesiredValue){
        // add it on a temporary layer
        // and display it
        // while hiding yourVectorLayer
    }
});

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.