简体   繁体   English

OpenLayers投影

[英]OpenLayers projection

I have a little problem with OpenLayers 3. I have the following script: 我对OpenLayers 3有点问题。我有以下脚本:

var map = new ol.Map({
    view : new ol.View({
        center : [5.611155, 52.238879],
        projection : 'EPSG:4326',
        zoom : 8.5,
        minZoom : 8.5,
        maxZoom : 12.5
    }),
    layers : [
        new ol.layer.Tile({
            source : new ol.source.OSM()
        }),

    ],
    target : 'map'
});

This should, if I am not mistaken, show a map centred on a place in The Netherlands. 如果我没记错的话,应该以荷兰的某个地方为中心显示地图。 But instead of showing a map, I only see blue. 但是我没有看到地图,而是看到了蓝色。 Even if I set the zoom to 1, there is no world to see. 即使将缩放比例设置为1,也看不到任何世界。

The problem doesn't seem to be there if I remove the projection attribute from the view. 如果我从视图中删除了projection属性,问题似乎就不存在了。 But then of course I should give all coordinates in another coordinate system, which is not possible because I depend on other systems as well. 但是当然我应该在另一个坐标系中给出所有坐标,这是不可能的,因为我也依赖于其他系统。

When I removed the projection attribute from the view attribute and loaded a GeoJSON file like this: 当我从view属性中删除projection属性并加载了如下的GeoJSON文件时:

new ol.layer.Vector({
  source: new ol.source.GeoJSON({
    projection: 'EPSG:4326',
    url: 'data/map.geojson'
  })
})

It should place a layer on top of The Netherlands, but instead the GeoJSON was shown somewhere in Africa. 它应该在荷兰的顶部放置一层,但是GeoJSON显示在非洲的某个地方。

Can someone help me? 有人能帮我吗?

Openstreetmap has a different projection than OL3. Openstreetmap的投影与OL3不同。 OSM uses EPSG:900913 and when you uses it as background you have to use it as your main projection. OSM使用EPSG:900913,当您将其用作背景时,必须将其用作主要投影。

This will work for the first part of your problem: 这将解决您问题的第一部分:

var centerpos = [5.611155, 52.238879]; // Your original position in LatLon
var newpos = ol.proj.transform(centerpos,'EPSG:4326','EPSG:900913');
var map = new ol.Map({
    view : new ol.View({
        projection : 'EPSG:900913', // OSM projection
        center : newpos,
        zoom : 8.5,
        minZoom : 8.5,
        maxZoom : 12.5
    }),
    layers: [
        new ol.layer.Tile({
               source: new ol.source.OSM()
            })
         ],
   target : 'map'
});

For your last problem about GeoJson I think it will work because you set the projection (eventually set the projection in the source), but I haven't tested it. 对于您有关GeoJson的最后一个问题,我认为它会起作用,因为您设置了投影(最终在源文件中设置了投影),但是我尚未对其进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM