Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transforming GPS coordinates for OpenLayers

I am building a project in JavaScript that uses OpenLayers with an OpenStreetMap layer.

I have the coordinates "48.3185005, 14.2853003" (the coordinated of Linz, Austria) which I get from either navigator.geolocation.getCurrentPosition() or the Google Maps Geocoding API. Now I want to show this point on the map.

Which transformations are necessary for the map to display the correct location and how do I do these transformations with OpenLayers? At the moment, I am always getting some point east of Africa.

like image 638
Manuel Hoffmann Avatar asked Nov 26 '25 16:11

Manuel Hoffmann


1 Answers

This is because OpenStreetMap data is a projected coordinate system, known as Web Mercator, ie, it is in meters, whereas your GPS data are in lat/lon. This is why all points appear to be in the sea off the West coast of Africa, as in a coordinate system covering the whole globe in meters, any coordinates in the range 180,180 and -90,90 will appear to be in that small area off the coast of Ghana. I notice in your original post you said East of Africa, but in a comment, you said Atlantic Ocean, from which I am assuming you meant West, which is consistent with my explanation. Is this correct?

You can deal with this in you map constructor by specifying a different map projection and display projection: see http://docs.openlayers.org/library/spherical_mercator.html

You want something along the lines of:

var map = new OpenLayers.Map("map", {
  projection: new OpenLayers.Projection("EPSG:900913"),
  displayProjection: new OpenLayers.Projection("EPSG:4326")
});

EPSG:3857 (originally 900913) is the official designation for Spherical Mercator and conversion between this and 4326 (lat/lon) is built into OpenLayers.

like image 56
John Powell Avatar answered Nov 28 '25 04:11

John Powell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!