简体   繁体   中英

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.

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.

仅仅是因为您正在混合这些值,Point(14.2853003,48.3185005)在也门,而Point(48.3185005,14.2853003)在奥地利。因此,请检查文档是否以与您不同的顺序给出了纬度和经度值考虑到

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM