0

I want to add a MousePosition control, without using a HTML-control as target, but just put it in a variable.

    map.addControl(new ol.control.MousePosition({
        coordinateFormat: function(coordinate) {
            return ol.coordinate.format(coordinate, 'Lat: {y}, Long: {x}', 4);
        },
        className: 'coordinate_display',
        change: function(evt){
                 console.log(evt); //or anything to catch the event
                },
        projection: 'EPSG:4326',
    }));

Is it the right way to listen to the mousposition ? I dont get into to the change-handler.

1
  • 1
    You could use the map.on ('pointermove', ) event
    – Mike
    Jan 26 '21 at 20:21
1

ol.control.MousePosition control has no change option (see https://openlayers.org/en/latest/apidoc/module-ol_control_MousePosition-MousePosition.html), but you can 'misuse' coordinateFormat option function for your purpose.

Code could then look something like this:

map.addControl(new ol.control.MousePosition({
  coordinateFormat: function(coordinate) {
    console.log(coordinte);  // displaying coordinate at each change
    return ol.coordinate.format(coordinate, 'Lat: {y}, Long: {x}', 4);
  },
  projection: 'EPSG:4326',
}));
-1

Actually, I found a solution like @mike mentioned. But thanks anyway:

https://stackoverflow.com/questions/49659736/capturing-mousemove-for-a-single-layer-in-openlayers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.