Openlayers OSM 始终设置为开启(经/纬)(0/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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您尝试了多个投影并且所有投影都导致相同的结果,则可能是因为 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)