Openlayers 矢量切片 VectorLayer/VectorSource

2 篇文章 0 订阅

效果图

代码 

<template>
  <div ref="mapEl" :style="{ width: 100vw, height: 100vh}"></div>
</template>
  
<script setup>
import "ol/ol.css";
import TileLayer from "ol/layer/Tile"; // 切片
import VectorLayer from "ol/layer/Vector"; // 矢量
import VectorSource from "ol/source/Vector";
import { Map, View, Feature } from "ol";
import { XYZ } from "ol/source";
import { Style, Stroke, Fill } from "ol/style";
import { Polygon, MultiPolygon } from "ol/geom";
import { fromLonLat } from "ol/proj";
import { defaults } from "ol/control";

import areaGeo from "./vtj.json";


const mapEl = ref(null);
const state = reactive({
  map: null,
  areaLayer: null,
});

onMounted(() => {
  init();
  addArea(areaGeo);
});

// 初始化地图
const init = () => {
  state.map = new Map({
    target: mapEl.value,
    controls: new defaults({
      zoom: false,
    }),
    view: new View({
      center: ["105.3683244", "36.915085"],
      projection: "EPSG:4326",
      zoom: 5,
      minZoom: 4,
      maxZoom: 18,
    }),
  });

  var gaodeMapLayer = new TileLayer({
    customData: "background",
    source: new XYZ({
      url: "http://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}",
    }),
    zIndex: 0,
  });

  state.map.addLayer(gaodeMapLayer);
  state.map.getView().setCenter([116.395645038, 39.9299857781]);
};

// 设置区域
const addArea = (geo = []) => {
  if (geo.length == 0) return false;
  // 设置图层
  state.areaLayer = new VectorLayer({
    source: new VectorSource({
      features: [],
    }),
  });

  let areaFeatureArray = [];
  // 添加图层
  state.map.addLayer(state.areaLayer);
  const style = new Style({
    fill: new Fill({ color: "#4e98f4" }),
    stroke: new Stroke({
      width: 3,
      color: [71, 137, 227, 1],
    }),
  });

  geo.forEach((g) => {
    areaFeatureArray = g.features.map((lineData, index) => {
      if (lineData.geometry.type == "MultiPolygon") {
        return new Feature({
          geometry: new MultiPolygon(lineData.geometry.coordinates),
          style: style,
        });
      } else if (lineData.geometry.type == "Polygon") {
        return new Feature({
          geometry: new Polygon(lineData.geometry.coordinates),
          style: style,
        });
      }
    });
  });

  state.areaLayer.getSource().addFeatures(areaFeatureArray);
};
</script>

import areaGeo from "./vtj.json"; 这个文件需要去数据源 , 下载JSON API,

有问题留评论区,工作日看到会回复的

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
以下是使用Vue和OpenLayers初始化VectorLayer的示例代码: ```vue <template> <div id="map"></div> </template> <script> import 'ol/ol.css'; import { Map, View } from 'ol'; import { Tile as TileLayer, Vector as VectorLayer } from 'ol/layer'; import { OSM, Vector as VectorSource } from 'ol/source'; import { fromLonLat } from 'ol/proj'; import Feature from 'ol/Feature'; import Point from 'ol/geom/Point'; import { Icon, Style } from 'ol/style'; export default { name: 'Map', mounted() { const map = new Map({ target: 'map', layers: [ new TileLayer({ source: new OSM(), }), ], view: new View({ center: fromLonLat([0, 0]), zoom: 2, }), }); const pointLayer = new VectorLayer({ source: new VectorSource(), }); map.addLayer(pointLayer); const coordinates = [ [0, 0], [10, 10], [20, 20], ]; const featuresArr = []; coordinates.forEach((coordinate) => { const feature = new Feature({ geometry: new Point(fromLonLat(coordinate)), }); feature.setStyle( new Style({ image: new Icon({ src: 'https://openlayers.org/en/latest/examples/data/icon.png', }), }) ); featuresArr.push(feature); }); pointLayer.getSource().addFeatures(featuresArr); }, }; </script> <style> #map { height: 400px; } </style> ``` 上述代码中,我们首先在Vue组件中引入了OpenLayers相关的模块,然后在mounted钩子函数中初始化了一个地图,并添加了一个OSM图层。接着,我们创建了一个VectorLayer矢量图层,并将其添加到地图中。然后,我们定义了一组经纬度坐标数据,并循环遍历这些坐标数据,创建了一个点要素Feature,并设置了其样式。最后,我们将所有的要素信息添加到了pointLayer图层的数据源中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晚风吹心事

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值