获取已注册事件的事件

时间:2014-06-10 09:14:15

标签: javascript openlayers

我是地图和OpenLayers的新手,但是我正在研究Openlayers,因为我在下一个项目中需要地图功能。该地图是中世纪城镇的WMS图像,但没有任何地理参考信息。

我找到了如何注册事件,但问题是“eventargs”没有像我找到的例子那样工作。在其中一个例子中,他们在用户淘汰之后得到x和y值:

map.events.register('moveend', map, function (e)
{
    alert(e.xy);
});

如果我在Visual Studio中尝试此操作,则e没有xy属性。我错过了什么?这是我现在的代码:

<script type="text/javascript">         

        var map, layer;

        function init() {
            var windowHeight = $(window).height();
            var windowWidth = $(window).width();
            var mapdiv = $('#map');
            mapdiv.css({width: windowWidth + 'px', height: windowHeight + 'px'});

            map = new OpenLayers.Map('map', { maxResolution: 1000 });
            layer = new OpenLayers.Layer.Image(
               'Globe ESA',
               '[url]',
               new OpenLayers.Bounds(-180.0, -12333.5, 21755.5, 90.0),
               new OpenLayers.Size(windowWidth, windowHeight),
               {numZoomLevels: 100}
            );
            map.addLayer(layer);
            nav = new OpenLayers.Control.Navigation();
            map.addControl(nav);
            //events test
            map.events.register('moveend', map, function (e)
            {
                alert(e.xy);
            });
            map.zoomToMaxExtent();
        }
    </script>

在OpenLayers示例中,他们不使用eventargs,但我认为必须有一种方法可以获得zoomlevel,或者在平移后获得x和y?

谢谢!

1 个答案:

答案 0 :(得分:0)

没有向moveend事件发送显式xy作为侦听器的参数。所以,虽然你可以写,

map.events.register('moveend', map, function(xy){

xy不包含任何有用的信息。

但是,您可以从OpenLayers.Map本身获取信息,例如maxExtent和zoom,它们将作为对象传递给moveend事件。

 map.events.register('moveend', map, function(){           
    console.log(map.maxExtent + " " + map.getZoom());
 });

还有许多与地图关联的其他属性,如果在moveend回调中设置断点,则可以看到这些属性。如果您还没有查看地图的来源,那么了解正在发生的事情是非常值得的。 OpenLayers.Map.js

您将看到moveend事件是在没有任何参数的情况下触发的,或者是一个boolean,zoomChanged。

相关问题