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,

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

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

温柔于心动

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

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

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

打赏作者

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

抵扣说明:

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

余额充值