# openlayers

# 设置高亮显示关键代码

if (this.resultVterSource.clear) {
  this.resultVterSource.clear();
}
const feature = this.vectorSource.getFeatureById(item.id);
this.resultVterSource.addFeature(feature);
// 设置显示区域偏移
this.getMap.getView().fit(this.resultVterSource.getExtent(), {
  padding: [0, 0, 0, document.body.clientWidth * 0.5]
});

# 取消地图双击放大事件

      const dblClickInteraction = this.map.getInteractions().getArray().find(interaction => {
        return interaction instanceof DoubleClickZoom
      })
      dblClickInteraction.setActive(false)
      // this.map.removeInteraction(dblClickInteraction)

# 生成凸包多边形代码:

    import Polygon from 'ol/geom/Polygon'
		import VectorLayer from 'ol/layer/Vector'
		import VectorSource from 'ol/source/Vector'
      // 测试代码
      const vector = new VectorLayer({ source: new VectorSource() })
      const hull = new Feature(new Polygon([[0, 0]]))
      vector.getSource().addFeature(hull)
      this.map.addLayer(vector)
      const pts = [
        [114.220879661414, 22.975200845151],
        [114.220860930313, 22.975184518253],
        [114.220860930313, 22.975184518253],
        [114.220828365764, 22.9752191658701],
        [114.220828365764, 22.9752191658701],
        [114.220833528445, 22.9752275112574],
        [114.220833528445, 22.9752275112574],
        [114.220821206418, 22.9752423824677],
        [114.220828365764, 22.9752191658701],
        [114.220720072206, 22.9751794500514],
        [114.220720072206, 22.9751794500514],
        [114.220658387028, 22.9754358833445],
        [114.220658387028, 22.9754358833445],
        [114.220620679617, 22.9755946167627],
        [114.220658387028, 22.9754358833445],
        [114.22065148011, 22.9754344030389],
        [114.220620679617, 22.9755946167627],
        [114.220531663839, 22.9759503527799],
        [114.220531663839, 22.9759503527799],
        [114.22040763731, 22.9764204097126],
        [114.22040763731, 22.9764204097126],
        [114.220397895536, 22.9764448850231],
        [114.220397895536, 22.9764448850231],
        [114.220394308619, 22.9764520687121]
      ]
      hull.setGeometry(new Polygon([this.convexHull(pts)]))
      this.map.getView().fit(vector.getSource().getExtent())
     convexHull(points) {
      let i
      points.sort(function (a, b) {
        return a[0] === b[0] ? a[1] - b[1] : a[0] - b[0]
      })
      console.log(points)
      const lower = []
      for (i = 0; i < points.length; i++) {
        while (lower.length >= 2 && this.clockwise(lower[lower.length - 2], lower[lower.length - 1], points[i])) {
          lower.pop()
        }
        lower.push(points[i])
      }
      const upper = []
      for (i = points.length - 1; i >= 0; i--) {
        while (upper.length >= 2 && this.clockwise(upper[upper.length - 2], upper[upper.length - 1], points[i])) {
          upper.pop()
        }
        upper.push(points[i])
      }

      upper.pop()
      lower.pop()
      return lower.concat(upper)
    },
    clockwise(a, b, o) {
      return ((a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0]) <= 0)
    },

评 论:

更新: 11/21/2020, 7:00:56 PM