eventsevent-handlingopenlayersmouseuponmouseup

Can't get lon-lat from mouseUp event after dragging element to map (openlayers)


Problem encountered while trying to get coords of a point where mouseUp event happens. Here is an event handler:

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

And here is a trigger:

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)));
    }
});

I got Uncaught TypeError: Cannot read property 'lat' of null So, how can I get lat and lon of point, where mouseUp event was registered?


Solution

  • you should send an object with x and y properties to getLonLatFromPixel method. So in your case it will look like this: var marker = map.getLonLatFromPixel({x:e.pageX, y:e.pageY});