Openlayers OSM 始终设置为开启(经/纬)(0/0)

发布于 2024-12-19 02:46:03 字数 968 浏览 3 评论 0原文

这是我的代码

map = new OpenLayers.Map("map");    
// I tried several projections here, all lead to the same result
var proj = new OpenLayers.Projection("WGS84");
var point = new OpenLayers.LonLat(position.coords.latitude,position.coords.longitude);
point.transform(proj, map.getProjectionObject());
    
// the output of this shows the correct coordinates
console.log("latitude: "+position.coords.latitude+"  long "+position.coords.longitude);
    
var mapnik = new OpenLayers.Layer.OSM();
map.addLayer(mapnik);
map.setCenter(point,3);

它总是显示以坐标(0/0)为中心的地图,该坐标位于大西洋的某个地方。我的错误在哪里?我无法解决这个问题并且在谷歌上找不到任何东西。

然而,这确实有效。

map = new OpenLayers.Map( 'map');
layer = new OpenLayers.Layer.OSM( "Simple OSM Map");
map.addLayer(layer);
map.setCenter(
  new OpenLayers.LonLat(position.coords.latitude,position.coords.longitude).transform(
  new OpenLayers.Projection("EPSG:4326"),
  map.getProjectionObject()
  ), 12
); 

this is my code

map = new OpenLayers.Map("map");    
// I tried several projections here, all lead to the same result
var proj = new OpenLayers.Projection("WGS84");
var point = new OpenLayers.LonLat(position.coords.latitude,position.coords.longitude);
point.transform(proj, map.getProjectionObject());
    
// the output of this shows the correct coordinates
console.log("latitude: "+position.coords.latitude+"  long "+position.coords.longitude);
    
var mapnik = new OpenLayers.Layer.OSM();
map.addLayer(mapnik);
map.setCenter(point,3);

It always shows the map centered on coordinate (0/0) which is somewhere in the Atlantic Ocean. Where is my error? I cant solve this and find nothing on Google.

This, however, works.

map = new OpenLayers.Map( 'map');
layer = new OpenLayers.Layer.OSM( "Simple OSM Map");
map.addLayer(layer);
map.setCenter(
  new OpenLayers.LonLat(position.coords.latitude,position.coords.longitude).transform(
  new OpenLayers.Projection("EPSG:4326"),
  map.getProjectionObject()
  ), 12
); 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

々眼睛长脚气 2024-12-26 02:46:03

如果您尝试了多个投影并且所有投影都导致相同的结果,则可能是因为 Proj4js 未正确加载(请参阅 http://trac.osgeo.org/openlayers/wiki/Documentation/Dev/proj4js

另请检查此示例:http://openlayers.org/dev/examples/osm.html (坐标重新投影每股收益:4326)

If you tried several projections and all lead to the same result that's probably because Proj4js is not correctly loaded (see http://trac.osgeo.org/openlayers/wiki/Documentation/Dev/proj4js)

Check also this example: http://openlayers.org/dev/examples/osm.html (coords re-projection from EPSG:4326)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文