最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to convert "normal" coordinates to OSM coordinates in OpenLayers - Stack Overflow

programmeradmin3浏览0评论

I am quite new to OpenLayers, and already stuck on my first problem. I am trying to convert "normal" coordinates, which I copied from Google Maps to OSM ones to center my map to a certain area. Now as far as i understood ist the "normal" coordinate system EPSG:4326 and the OSM one is EPSG:3857 or EPSG:900913, which is the same. so my Javascript code is:

map.setCenter(new OpenLayers.LonLat(53.537460, 9.953227).transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:3857")), 5);

But this centers to a total different location. what am I missing?

I am quite new to OpenLayers, and already stuck on my first problem. I am trying to convert "normal" coordinates, which I copied from Google Maps to OSM ones to center my map to a certain area. Now as far as i understood ist the "normal" coordinate system EPSG:4326 and the OSM one is EPSG:3857 or EPSG:900913, which is the same. so my Javascript code is:

map.setCenter(new OpenLayers.LonLat(53.537460, 9.953227).transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:3857")), 5);

But this centers to a total different location. what am I missing?

Share Improve this question asked May 9, 2014 at 14:56 olkozaolkoza 7252 gold badges17 silver badges36 bronze badges 4
  • Does it center correctly if you leave the transformation bit out? – Genti Saliu Commented May 9, 2014 at 15:01
  • No, it solves to another ("wrong") location – olkoza Commented May 9, 2014 at 15:06
  • And where does it suppose to set the center? – D.Evangelista Commented May 9, 2014 at 15:54
  • 2 I'm not sure I would use the term normal. 4326 means latitude and longitude (or more correctly, longitude/latitude, as scai has pointed out), while 3857 is spherical mercator, projected in meters. – John Powell Commented May 9, 2014 at 16:52
Add a ment  | 

2 Answers 2

Reset to default 5

Google Maps and OSM show exactly the same position. Everything else would be odd.

However you are mixing up lat and lon. OpenLayers.LonLat() accepts first lon, then lat as the name already tells you. But you are passing lat first and then lon, which will result in a pletely different position of course.

Is your location in Steinwerder (Hamburger Hafen) or off the Horn of Africa? I assume the first so the Latitude would be 53 degree north and the longitude 9 degree west.

This should set your map in the right location.

map.setCenter(new OpenLayers.LonLat(9.953227,53.537460).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.

发布评论

评论列表(0)

  1. 暂无评论