This issue is related to Projection used by base layers. WMS layer is using projection of EPSG:4326. This projection consider earth as ellipsoid and contains bound in degree of -180.0000, -90.0000, 180.0000, 90.0000
While OSM and other well known maps like Google Maps, Bing Maps uses EPSG:900913 (Officially known as EPSG:3857). This is mercator projection which consider earth as sphere instead of ellipsoid and contains bounds in meter from -20037508.34 to 20037508.34.
So when you change your layer from WMS to OSM; projection changes to EPSG:900913 and in EPSG:900913, difference between points added by you is so negligible ( as they are considered as meters instead of degrees) they are projected near 0,0. (Go for full zoom to look at difference between points.)
According to me, to resolve this issue you have to transform your points through OpenLayers.LonLat.transform method when base layer is changed. You can associate "changebaselayer" event to raise event when your base layer is changed.
For more clarity I have modified jsfiddle provided by Andreas with following changes
- Added label displaying projection of WMS and OSM layer. (for reference only)
Associated changebaselayer method with map object
map = new OpenLayers.Map('map', { eventListeners: { "changebaselayer": mapBaseLayerChanged } });
Added method "" in which you can write logic to change projection of marksers by calling transform method.
function mapBaseLayerChanged(event) { var currentLayer = event.layer; alert(currentLayer.name); // Write code to transform each markers. }
Hope this answer helps you.