1

To be blunt, I'm new to OpenLayers. I have a database of WGS84 coordinates which I've loaded into a database. I've verified that this works by echoing latitudes and longitudes.

map = new OpenLayers.Map("map");
var mapnik = new OpenLayers.Layer.OSM();
map.addLayer(mapnik);
map.setCenter(new OpenLayers.LonLat(<?php echo $lat;?>, <?php echo $lon;?>).transform(
   new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
   new OpenLayers.Projection("EPSG:900913") // to Spherical Mercator Projection
), 4 /*Zoom level*/);

When putting into here, it moves the map to 0,0. Can anyone help or point me in the right direction?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Mark Iliffe
  • 417
  • 5
  • 17
  • 1
    Where are you setting `$lat` and `$long`? It would help to see that... – Alex Oct 20 '11 at 00:21
  • Your Lat&Lon values either not set or not in 4326 projection.What about their values? – Myra Oct 20 '11 at 06:32
  • 4
    Is the order of your coordinates correct? Your arguments are $Lat,$Lon, while OpenLayer.LonLat, expects $Lon,$Lat. – Niels Oct 20 '11 at 07:45
  • I have the same problem see [Position][1] [1]: http://stackoverflow.com/questions/20346172/openlayers-after-call-setcenter-map-is-still-on-0-0-position – Musketyr Dec 09 '13 at 14:09

1 Answers1

0

The commend by @Niels is correct.
The OpenLayers LonLat object expects (longitude, latitude) - not (latitude, longitude).

Just swap the order of your lat and lon:

map.setCenter(new OpenLayers.LonLat(<?php echo $lon;?>, <?php echo $lat;?>).transform(
    new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
    new OpenLayers.Projection("EPSG:900913") // to Spherical Mercator Projection
), 4);
sfletche
  • 47,248
  • 30
  • 103
  • 119