2

I have a problem centering the map in OpenLayers 3:

When i click the button to center the map, the map dissapears, this only happen with the version 3.19.1 of openlayers. The same code with vesion 3.17.1 works well.

Why is happenning this?

Fiddle 3.19.1 (Map dissapear)

Fiddle 3.17.1 (Works)

  • Looks like it has something to do with the coordinates... take a look at this fiddle where I've put the map in 4326 and transform your 3857 coordinates in 4326: jsfiddle.net/wo9uvw16/2 – kaycee Nov 8 '16 at 16:22
  • Thank you for your answer, your example works perfect, i made some changes to the code to work with 3857 but stills don't work, is really weird: https://jsfiddle.net/wo9uvw16/3/ – Hassim Nov 8 '16 at 18:10
  • Both work for me. It is likely to be some kind of system specific bug. Do you see an errors in the Javascript console? – John Powell Nov 8 '16 at 20:27
  • The console is not reporting any error, when you click the center button the map dissapears, but the controls of the zoom and atributtions still there, is weird... – Hassim Nov 9 '16 at 15:05
3

I found the problem of the map dissapear.

When you use the javascript function split and pass to the function a string, it creates an array of strings.

Example:

var lonlat= "-103,24";
var lonlat_array = lonlat.split(",");
// Create: ["-103", "24"]

So, the setCenter function of OpenLayers 3 expects an array of numbers, when receive an array of strings the map dissapears.

To fix this issue i need to create an array of numbers this way:

var lonlat= "-103,24";
var lonlat_array = lonlat.split(",").map(Number);
// Create: [-103, 24]

Then when i use the setCenter it works perfect:

https://jsfiddle.net/wo9uvw16/4/

In the fiddle i send to the console both cases to see the diferences, thanks all of you for the help!

  • You just saved my life – TeChn4K Jun 30 '17 at 14:51

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.