如何在openlayers中设置多边形的坐标?

时间:2016-12-30 10:18:19

标签: coordinates polygon openlayers-3

我是OpenLayers和JavaScript的新手,我有以下问题。

我有一个.csv表,表示我希望使用OpenLayers在地图上显示它们的点的坐标。

我在OpenLayers页面上找到了以下示例,

https://openlayers.org/en/latest/examples/polygon-styles.html

然而,我无法理解那里坐标的表示。更具体地说,我不知道e在坐标[-5e6, 6e6]中的含义是什么。

然而,我尝试使用我的坐标在我的地图上绘制一个简单的正方形,但它只是给我一个点,位于地图的中心,如下所示:

https://jsfiddle.net/api/post/library/pure/#&togetherjs=bD5bfPm7vz

所以我不知道究竟是什么问题,我该怎么改变?我认为这些都是坐标编写的方式,但不确定。

这是我的代码:

var styles = [
  new ol.style.Style({
    stroke: new ol.style.Stroke({
      color: 'blue',
      width: 3
    }),
    fill: new ol.style.Fill({
      color: 'rgba(0, 0, 255, 0.1)'
    })
  }),
  new ol.style.Style({
    image: new ol.style.Circle({
      radius: 5,
      fill: new ol.style.Fill({
        color: 'orange'
      })
    }),
    geometry: function(feature) {
      // return the coordinates of the first ring of the polygon
      var coordinates = feature.getGeometry().getCoordinates()[0];
      return new ol.geom.MultiPoint(coordinates);
    }
  })
];

var geojsonObject = {
  'type': 'FeatureCollection',
  'crs': {
    'type': 'name',
    'properties': {
      'name': 'EPSG:3857'
    }
  },
  'features': [{
    'type': 'Feature',
    'geometry': {
      'type': 'Polygon',
      'coordinates': [[[-5e6, 6e6], [-5e6, 8e6], [-3e6, 8e6],
          [-3e6, 6e6], [-5e6, 6e6]]]
    }
  }]
};

var source = new ol.source.Vector({
  features: (new ol.format.GeoJSON()).readFeatures(geojsonObject)
});

var layer = new ol.layer.Vector({
  source: source,
  style: styles
});

    var basic = new ol.layer.Tile({
      source: new ol.source.OSM()
    });

var map = new ol.Map({
  layers: [basic, layer],
  target: 'map',
  view: new ol.View({
    center: [0, 3000000],
    zoom: 2
  })
});

1 个答案:

答案 0 :(得分:0)

好的,我找到了答案。 以下坐标[-5e6, 6e6]采用X,Y格式,并基于EPSG:3857投影。 XeY等于X * 10 ^ Y。通常情况下,openlayers使用EPSG:3857投影,但为了使用经度/纬度坐标格式,我们必须使用投影:EPSG:4326投影,我们清楚地指定它:projection: 'EPSG:4326