拖动元素到map(openlayers)后,无法从mouseUp事件中获取lon-lat

时间:2013-06-13 11:30:55

标签: events event-handling openlayers mouseup onmouseup

尝试获取mouseUp事件发生点的坐标时遇到的问题。 这是一个事件处理程序:

$('#someDiv')
.on("mouseup", map, function(event) {
    click.trigger(event, map, d[i]);
})

这是一个触发器:

OpenLayers.Control.Click = OpenLayers.Class(OpenLayers.Control, {
    defaultHandlerOptions: {
        'single': true,
        'double': false,
        'pixelTolerance': 0,
        'stopSingle': false,
        'stopDouble': false
    },
    initialize: function(options) {
        this.handlerOptions = OpenLayers.Util.extend(
            {}, this.defaultHandlerOptions
        );
        OpenLayers.Control.prototype.initialize.apply(
            this, arguments
        );
        this.handler = new OpenLayers.Handler.Click(
            this, {
                'mouseup': this.trigger
            }, this.handlerOptions
        );
    },
    trigger: function(e, map,  k) {
        var marker = map.getLonLatFromPixel(e.xy);
        var point = new OpenLayers.Geometry.Point(new OpenLayers.LonLat(marker.lat.toFixed(5),
        marker.lng.toFixed(5)));
    }
});

我得到未捕获的TypeError:无法读取null的属性'lat' 那么,如何在注册mouseUp事件的地方获得lat和lon of point?

1 个答案:

答案 0 :(得分:2)

您应该将具有x和y属性的对象发送到getLonLatFromPixel方法。所以在你的情况下,它将如下所示:     var marker = map.getLonLatFromPixel({x:e.pageX,y:e.pageY});