javascript - Openlayers OSM 始终设置为 (long/lat) (0/0)

标签 javascript openlayers openstreetmap

这是我的代码

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 。我的错误在哪里?我无法解决这个问题,但在 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
); 

最佳答案

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

同时检查这个例子:http://openlayers.org/dev/examples/osm.html (坐标从 EPSG 重新投影:4326)

关于javascript - Openlayers OSM 始终设置为 (long/lat) (0/0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8338157/

相关文章:

javascript - 看不见的向量?组合 d3.tile()、d3.zoom() 和 TopoJSON 向量

javascript - 如何在javascript中组合onchange

javascript - 使用 JQuery 改变颜色

javascript - 如何在 openlayers 中使用 bing map 自己的样式

Javascript 语法(2 分钟问题)

android - Openstreetmap 隧道

javascript - jQuery UI 日期选择器 : add 6 months to another datepicker

javascript - AngularJS 表单提交不起作用

javascript - 如何隐藏和显示 OpenLayers 3 中的功能? (重绘?)

sql - postgis地址查询(带有osm数据)