在 OpenLayers 地图中显示多边形

Spe*_*ath 3 javascript openlayers-3

我在(经度,纬度)点中有一个多边形,我想绘制:

      var maxPoint = [36.283, -114.368];
      var geoSquare = [ minPoint, [minPoint[0], maxPoint[1]], maxPoint, [maxPoint[0], minPoint[1]]];
      var polygonFeature = new Feature(
              new Polygon(geoSquare));
Run Code Online (Sandbox Code Playgroud)

我正在以下列方式绘制地图:

      var map = new Map({
        interactions: defaultInteractions().extend([new Drag()]),
        layers: [
          new TileLayer({
            source: new TileJSON({
              url: 'https://maps.siemens.com/styles/osm-bright.json'
            })
          }),
          new VectorLayer({
            source: new VectorSource({
              features: [polygonFeature]
            }),
            style: new Style({
              stroke: new Stroke({
                width: 3,
                color: [255, 0, 0, 1]
              }),
              fill: new Fill({
                color: [0, 0, 255, 0.6]
              })
            })
          })
        ],
        target: 'map',
        view: new View({
          center: [0, 0],
          zoom: 2
        })
      });
Run Code Online (Sandbox Code Playgroud)

这个多边形靠近南加州,但我在地图上根本看不到这个正方形。怎么了??

编辑

这是一个jsfiddle

Mik*_*ike 5

要素坐标必须采用 LonLat 格式,并且必须转换为视图投影。多边形环必须在同一点开始和结束,多边形需要额外的一对,[]因为它们可以有额外的内部环(孔)。或者,您可以从范围创建多边形。这些中的任何一个都会起作用。

  var minPoint = [-121.091, 32.92];
  var maxPoint = [-114.368, 36.283];
  var geoSquare = [[minPoint, [minPoint[0], maxPoint[1]], maxPoint, [maxPoint[0], minPoint[1]], minPoint]];
  var polygonFeature = new Feature(
    new Polygon(geoSquare).transform('EPSG:4326','EPSG:3857'));



  var minPoint = [-121.091, 32.92];
  var maxPoint = [-114.368, 36.283];
  var polygonFeature = new Feature(
    Polygon.fromExtent(minPoint.concat(maxPoint)).transform('EPSG:4326','EPSG:3857'));


  var minPoint = fromLonLat([-121.091, 32.92]);
  var maxPoint = fromLonLat([-114.368, 36.283]);
  var geoSquare = [[minPoint, [minPoint[0], maxPoint[1]], maxPoint, [maxPoint[0], minPoint[1]], minPoint]];
  var polygonFeature = new Feature(
    new Polygon(geoSquare));


  var minPoint = fromLonLat([-121.091, 32.92]);
  var maxPoint = fromLonLat([-114.368, 36.283]);
  var polygonFeature = new Feature(
    Polygon.fromExtent(minPoint.concat(maxPoint)));
Run Code Online (Sandbox Code Playgroud)