2

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.

cmca
  • 367
  • 1
  • 4
  • 14

2 Answers2

5

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 });
    map.setView(myView);
}

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

 <button onclick="myZoom()">Click me to zoom!</button> 
PolyGeo
  • 65,136
  • 29
  • 109
  • 338
David
  • 61
  • 1
  • 4
2

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

https://openlayersbook.github.io/ch09-taking-control-of-controls/example-03.html

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.

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