Motivation

If you set an initial center point for a map, it is only saved correctly at the first save. Every later save of the map will change the position slightly, if you do not reset the center point.

How to reproduce:

  • To easier see the error, go to Structure > OpenLayers and make sure, that "Preview map" is checked.
  • Add a new map at Structure > OpenLayers > Maps > Add.
  • Enter map title "Test".
  • Switch to "Layers & styles" tab. Make sure that MapQuest OSM is active and set as default map (it actually doesn't matter, which map you choose, as long as some map is active).
  • Switch to "Center & bounds" tab.
  • As centerpoint enter: 4.890443985625159, 52.37019700877677 (you can of course reproduce with any other position as well)
  • As zoom level enter: 12 (you can choose another level, but higher levels will make the bug more obvious)
  • Click "Save and edit".
  • If you enabled preview of the map earlier, you'll notice, that the map at the top is now centered on the host city of the next DrupalCon in Europe.
  • Switch to the "Center & bounds" tab again. You'll see that the "center help map" is not centered at the same position as the preview map at the top. The form value of the centerpoint field is now 4.921874991931841, 52.26815737142193, which is wrong.
  • Click "Save and edit" again. Your map displayed at the top is now centered on the wrong center point.

Discussion

The mapformvalues behavior is responsible for setting the correct center and zoom for the help map and applying any updates made on the map to the form. The problem lies with the zoomTo method used there. It somehow slightly changes the position during zoom (tested in OpenLayers 2.13.1) and the form value is updated with the wrong position. zoomTo has a second parameter to provide a LonLat object as the source of the zoom. If you do this, the map remains centered at the correct position. However, according to OpenLayers source code, this parameter is not part of the official OpenLayers API. The setCenter method allows adding a second parameter for the zoom level, which is part of the official API.

The downside of using setCenter is, that setCenter will not trigger the update of the form, since the map is not animated during zoom like with zoomTo and thus the moveend event is not triggered. I don't think, this is a problem though, as openlayers.js provided by the OpenLayers module also uses setCenter to set initial center and zoom and the value is probably converted to the correct projection beforehand like it is in the behavior – although I didn't check that. I also think, that it is a good idea to use the same method for setting center and zoom in both cases.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

FeyP’s picture

Patch against 7.x-2.x-dev

  • Pol committed e763e33 on 7.x-2.x authored by FeyP
    Issue #2302169 by FeyP: Initial centerpoint not preserved during...
Pol’s picture

Status: Needs review » Fixed

Good catch and patch ! :)

Thank you very much for this, your help is greatly appreciated !!!

FeyP’s picture

Well, thank you for taking my patches and for all the work you put into OpenLayers. After all, I'm just a happy user and it makes my life as a developer a lot easier, if I don't have to maintain a huge patch set. Also, your bug turnaround time is amazing. It is a lot of fun to work with upstream, if your patches are not bitrotting for ages. :)

Pol’s picture

Hello @FeyP,

I put many hours into OpenLayers these last years and it's always very kind to see people contributing, this is why I'm trying to be very responsive, so we can make it evolve.

Feel free to submit patches and ideas, they are always welcome.

Thanks once again.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.