在OpenLayers OSM()地图上添加三角形多边形

时间:2018-08-21 14:12:10

标签: javascript openlayers

我正在尝试在OpenLayers的OSM()地图上添加一个三角形多边形。

import Map from 'ol/Map';
import View from 'ol/View';
import Feature from 'ol/Feature';
import {
    OSM,
    Vector as VectorSource
} from 'ol/source';
import $ from 'jquery';
import {
    Tile as TileLayer,
    Vector as VectorLayer
} from 'ol/layer';
import {
    fromLonLat,
    toLonLat
} from 'ol/proj';
import Polygon from 'ol/geom/Polygon';

import {
    Circle as CircleStyle,
    Fill,
    Stroke,
    Style
} from 'ol/style.js';


//Position of our map center

var pos = fromLonLat([76.87403794962249, 8.569385045000772]);

//Position for our Polygon

var pos1 = fromLonLat([76.85860825505787, 8.575525035547585]);

var pos2 = fromLonLat([76.85286067404068, 8.56925661298456]);

var pos3 = fromLonLat([76.86300346314657, 8.56917303421666]);


//OSM() Tile layer for our Map
var tileLayer = new TileLayer({
    source: new OSM()
});

//Setting View for our Map
var view = new View({

    center: pos,
    zoom: 15
});

var cord = [pos1,pos2,pos3];

var polyone = new Polygon([cord]);

var featureone = new Feature(polyone);

var vectorSource = new VectorSource({
    feature: featureone
});

//vectorSource.addFeature(feature);
featureone.setStyle(new Style({fill: new Fill({color: 'red'})}));

var vectorLayer = new VectorLayer({
    source: vectorSource
});

var map = new Map({

    layers: [tileLayer,vectorLayer],
    target: 'map',
    view: view
});

我已经分别添加了html部分。

OpenLayers版本:^ 5.1.3 我正在使用Parcel Bundler创建我的测试版本 运行上述代码后,仅显示地图,而没有显示多边形。

有人可以建议对我的代码进行编辑吗?

1 个答案:

答案 0 :(得分:1)

VectorSource应该具有features属性(而不是feature),并且应使用一系列功能初始化该属性:

var vectorSource = new VectorSource({
    feature: featureone
});

应为:

var vectorSource = new VectorSource({
    features: [featureone]
});

也需要关闭多边形的路径(最后一点必须与第一个点相同:

var cord = [pos1, pos2, pos3, pos1];

screenshot of resulting map

概念验证代码段:

html,
body {
  height: 100%;
  width: 100%;
  padding: 0px;
  margin: 0px;
}

.map {
  height: 100%;
  width: 100%;
}
<link rel="stylesheet" href="https://openlayers.org/en/v5.1.3/css/ol.css" type="text/css">
<script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.1.3/build/ol.js"></script>

<div id="map" class="map"></div>
<script>
  //Position of our map center
  var pos = ol.proj.fromLonLat([76.87403794962249, 8.569385045000772]);
  //Position for our Polygon
  var pos1 = ol.proj.fromLonLat([76.85860825505787, 8.575525035547585]);
  var pos2 = ol.proj.fromLonLat([76.85286067404068, 8.56925661298456]);
  var pos3 = ol.proj.fromLonLat([76.86300346314657, 8.56917303421666]);

  //OSM() Tile layer for our Map
  var tileLayer = new ol.layer.Tile({ // TileLayer({
    source: new ol.source.OSM()
  });

  //Setting View for our Map
  var view = new ol.View({
    center: pos,
    zoom: 14
  });

  var cord = [pos1, pos2, pos3, pos1];

  var polyone = new ol.geom.Polygon([cord]);
  var featureone = new ol.Feature(polyone);
  var vectorSource = new ol.source.Vector({ // VectorSource({
    features: [featureone]
  });

  //vectorSource.addFeature(feature);
  featureone.setStyle(new ol.style.Style({
    fill: new ol.style.Fill({
      color: 'red'
    })
  }));

  var vectorLayer = new ol.layer.Vector({ // VectorLayer({
    source: vectorSource
  });

  var map = new ol.Map({
    layers: [tileLayer, vectorLayer],
    target: 'map',
    view: view
  });
</script>