I'm struggling to understand the coordinate system used by OpenLayers.
Leicester, UK is at approx.
Latitude: 52.63973017532399
Longitude: -1.142578125
But to display the same location using OpenLayers I have to use:
Latitude: 6915601.9146245
Longitude: -125089.1967713
e.g:
var center = new OpenLayers.LonLat(-125089.1967713, 6915601.9146245);
var map = new OpenLayers.Map("demoMap");
map.addLayer(new OpenLayers.Layer.OSM());
map.setCenter(center, 12);
These are clearly not Latitude-Longitude coordinates, is there some conversion I need to take account of?
A working example is http://craig-russell.co.uk/demos/openlayers/so_map.html
What projection is OpenLayers using? Every map that you'll create with OpenLayers will have a view, and every view will have a projection. As the earth is three-dimensional and round but the 2D view of a map isn't, we need a mathematical expression to represent it. Enter projections.
State Plane Coordinate System (SPCS) This coordinate system is highly accurate (four times as accurate as UTM).
(Google uses the World Geodetic System WGS84 standard.) World coordinates, which reference a point on the map uniquely.
It looks like I do need to map between coordinate systems.
This is done with the transform()
function like so:
var coor_from = new OpenLayers.Projection("EPSG:4326");
var coor_to = new OpenLayers.Projection("EPSG:900913");
var center = new OpenLayers.LonLat(-1.142578125, 52.63973017532399);
var map = new OpenLayers.Map("demoMap");
center.transform(coor_from, coor_to);
map.addLayer(new OpenLayers.Layer.OSM());
map.setCenter(center, 12);
Now it is possible to do:
var map = new OpenLayers.Map("demoMap");
var p = map.getView().getProjection();
var cord = ol.proj.fromLonLat([longitude, latitude], p);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With