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
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());
}
});
-
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());)– rendiCommented Oct 11, 2012 at 5:54
-
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())
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);
});