Javascript 缩放到openlayers中的搜索功能

Javascript 缩放到openlayers中的搜索功能,javascript,openlayers,Javascript,Openlayers,我创建了一个openlayers映射,并使用引导搜索控件。单击搜索按钮后,地图将显示缩放范围,但整个地图将再次刷新。 下面是我写的源代码 <div class="input-group add-on"> <input class="form-control" placeholder="Search" name="srch-term" id="srch-term" type="text" onkeypress="autoFill()"/>

我创建了一个openlayers映射,并使用引导搜索控件。单击搜索按钮后,地图将显示缩放范围,但整个地图将再次刷新。 下面是我写的源代码

<div class="input-group add-on">
          <input class="form-control" placeholder="Search" name="srch-term" id="srch-term" type="text" onkeypress="autoFill()"/>
          <div class="input-group-btn">
            <button class="btn btn-default" type="submit" onclick="searchStateName();"><i class="glyphicon glyphicon-search"></i></button>
          </div>
        </div>

function searchStateName() {

    input = document.getElementById("srch-term").value.toUpperCase();

    if (input) {
        for (var i = 0; i < countryData.length; i++) {
            //alert(countryData[0]);
            if (countryData[i].P.name_1.toUpperCase() == input) {
                alert(countryData[i].P.name_1);
                var extent = countryData[i].P.geom.getExtent();
                map.getView().fit(extent, map.getSize());

                if (countryData[i] !== highlight) {
                    if (highlight) {
                        featureOverlay.getSource().removeFeature(highlight);
                    }
                    if (countryData[i]) {
                        featureOverlay.getSource().addFeature(countryData[i]);
                    }
                    highlight = countryData[i];
                }

            }
        }
    }

}

函数searchStateName(){
input=document.getElementById(“srch term”).value.toUpperCase();
如果(输入){
对于(变量i=0;i

有人能帮我找到问题出在哪里吗

检查循环中的代码:


hightlight
是一个常见的变量,不是吗?它始终保留上次创建的特征。但是每次在循环中删除之前创建的特征时,您都会删除该特征,这样地图的末尾将只有一个特征。

运行示例,让您的问题跟进怎么样?
if (countryData[i] !== highlight) {
    if (highlight) {
        featureOverlay.getSource().removeFeature(highlight);
    }
    if (countryData[i]) {
        featureOverlay.getSource().addFeature(countryData[i]);
    }
    highlight = countryData[i];
}