【问题标题】:Openlayers OSM always set on (long/lat) (0/0)Openlayers OSM 始终设置为 (long/lat) (0/0)
【发布时间】:2011-12-01 08:04:47
【问题描述】:

这是我的代码

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) 为中心显示地图。我的错误在哪里?我无法解决这个问题,在 Google 上找不到任何东西。

不过,这行得通。

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
); 

【问题讨论】:

    标签: javascript openlayers openstreetmap


    【解决方案1】:

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

    也检查这个例子:http://openlayers.org/dev/examples/osm.html(来自 EPSG 的坐标重新投影:4326)

    【讨论】:

    • 第二个例子正在运行,请参阅我的帖子。但为什么?对我来说,它的作用基本相同。
    • 可能是WGS84 vs EPSG:4326
    • 嗯,就是这样。我以为我以前也尝试过...无论如何,非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    相关资源
    最近更新 更多