Openlayers OSM总是设置为(long/lat)(0/0)

Openlayers OSM always set on (long/lat) (0/0)

本文关键字:long lat OSM 设置 Openlayers      更新时间:2023-09-26

这是我的代码

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

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

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