Is it possible to get openlayers3 to center the map on the location of a click?

Don't have any code to show as i am just curious and can't seem to find any information regarding this.


A quick google search and I found this example to get the mouse coordinates:


and this example to set the center of the map:

Change OpenLayers 3 view center

After you implement this code, please feel free to post a new question.

  • thanks for that, will try implement later and if i have issues will post up. Cheers – cmca Sep 22 '16 at 1:35

I've just had the same issue and used @kttii's answer to solve it.

function myZoom() {
    myView = new ol.View({
        center: ol.proj.transform([ -46.604057, -23.542679 ],"EPSG:4326","EPSG:3857"), zoom: 11 });

Now, just add the call to myZoom() to your trigger (mine is a button):

 <button onclick="myZoom()">Click me to zoom!</button> 

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.