1

As far as I know OpenLayers can display our mouse coordinate position. However, I do not know how to display map center coordinate? Can anyone help me?

3 Answers 3

2

The easiest way would be to add an event to the map object so that on moveend some div (or something else) is updated. The map center itself can easily be retrieved from the map object with getCenter. For example:

map.events.on({
        "moveend":function(){
            alert(map.getCenter().toString());
        }
    });
2
  • Thanks om henners, it is very helpful for me. But, that I want really is that the map center coordinate automatically appear in map as like as mouse coordinate position automatically appear in map (map.addControl(new OpenLayers.Control.MousePosition());)
    – rendi
    Commented Oct 11, 2012 at 5:54
  • use map.getCenter() inside the moveend event. goo.gl/zUf6J
    – D_Guidi
    Commented Oct 11, 2012 at 6:48
1

You need to build a new Control, say OpenLayers.Control.MapCenter(). You can take OpenLayers.Control.MousePosition, rename it and change the registering of the events in the activate method. Remember also to change the unregistering in the deactivate method. Then you need to include js file with the definition of the new control and add it to you map: map.addControl(new OpenLayers.Control.MapCenter())

0

ol v 6.2.* try this:

map.on('pointerdrag', function() { 
  let centerPosArr = map.getView().getCenter();
  let coords = ol.proj.toLonLat(centerPosArr, 'EPSG:3857');  
  console.log(coords);
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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