用于控制Openlayers映射的外部超链接

时间:2013-10-09 09:11:39

标签: openlayers

我有一个基本地图定义如下面的显示(伪代码)。

在map.addLayers调用之后有一个displayData(theid)我想从html文件的主体调用,问题是我无法调用该方法。如果我将onclick事件绑定到< a>的id,我可以实现我的目标。标签,但是会有太多链接绑定到每个< a>标签

点击下面的一个超链接导致没有定义displayData()方法。

感谢阅读!

  <script>
        Ext.onReady(function() {
        map = new OpenLayers.Map();
        basemap = new OpenLayers.Layer.WMS(...);
        mylines = new OpenLayers.Layer.WMS(...);
        selected_items = new OpenLayers.Layer.Vector(...);
        map.addLayers([basemap, mylines, selected_items]);

        //attach an onclick even for the button on the search results page
        function displayData(theid) {
            OpenLayers.Request.GET({
                url: 'http://1.1.1.1:8080/geoserver/wfs?service=WFS&version=1.0.0&request=GetFeature&featureId='+theid,
                proxy: "../gis/geoserver.seam?url=",
                success: function(response) {
                //if were successful then add the returned features to the map and zoom to the extent
                var gmlReader = new OpenLayers.Format.GML({ extractAttributes: true });
                selected_items.removeAllFeatures();
                selected_items.addFeatures(gmlReader.read(response.responseText));
                map.zoomToExtent(selected_items.getDataExtent());
            }
        });
    }
}
</script>

<body>
    <div id="map" class="smallmap"></div>
    <br/><a href="" onlick="displayData('feature1');">View on map</a>
    <br/><a href="" onlick="displayData('feature2');">View on map</a>
    <br/><a href="" onlick="displayData('feature3');">View on map</a>
    <br/><a href="" onlick="displayData('feature4');">View on map</a>
</body>

1 个答案:

答案 0 :(得分:1)

JavaScript具有功能范围,这意味着displayData不是从外部“可见”

Ext.onReady(function() {
... stuff here is executed but is not visible from outside
}

要解决此问题,您可以在其外部声明一个假的空displayData,或者绑定链接的click事件(通过一个类)。