Search code examples
javascriptopenlayers

select feature in Openlayer is not working, selected feature doesn't highlight?


I manage to search the feature by its attribute..the problem is, it doesn't highlight the feature.. sample result

A popup will show but the selected feature does not highlight...here's my code below:

    this.showparcel = function(getpin){
        for(var f=0;f<layer_agao.features.length;f++) {
                    if(layer_agao.features[f].attributes.newpin === getpin) {
                        featsel = layer_agao.features[f];
                        selectControl.select(featsel);
                        break;
                    }
                    else{
                        selectControl.unselect(featsel);
                    }
                }
    }

Solution

  • It should work fine by using jquery and change selectControl.select(featsel); into selectControl.clickFeature(featsel); to avoid multiple selection of feature when searching..

    Here's the code:

    $("#forminput").change(function(){
            for (var f = 0; f < layer_agao.features.length; f++) {
                if (layer_agao.features[f].attributes.newpin == this.value) {
                    featsel = layer_agao.features[f];
                    $("#mybutton").click(function(){
                        selectControl.clickFeature(featsel);
                    });
                    break;
                }
            }
        });
    

    It should be placed inside you init() function, below is the html markup.

        <form class="navbar-form navbar-right">
                            {% csrf_token %}
                                <select name="q" class="form-control" id="forminput">
                                    <option type="text" value=""><b>Select Land PIN</b></option>
                                    {% for getlandpins in query_map %}
                                     <option type="text" value="{{ getlandpins.newpin }}">{{ getlandpins.newpin }}</option>
                                    {% endfor %}
                                </select>
                            <input type="button" class="btn btn-primary" value="Show Info" id="mybutton">
        </form>