Craig552uk
Craig552uk

Reputation: 681

OpenLayer Coordinate System

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

Upvotes: 11

Views: 6125

Answers (2)

boreq
boreq

Reputation: 850

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);

Upvotes: 3

Craig552uk
Craig552uk

Reputation: 681

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);

Upvotes: 9

Related Questions