问题
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.
回答1:
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.
回答2:
Would it simply be that you are mixing the values, the Point (14.2853003, 48.3185005) is in Yemen, and Point (48.3185005, 14.2853003) is in Austria..So check documentation whether the latitude & longitude values are given in different order than you think of
回答3:
This should set your map in the right location.
map.setCenter(new OpenLayers.LonLat(14.2853003, 48.3185005).transform('EPSG:4326', map.getProjectionObject()), 5);
You only need to know the input coordinate reference and use the getProjectObject function to get the target reference.
来源:https://stackoverflow.com/questions/23428170/transforming-gps-coordinates-for-openlayers