openlayers+vue 加载geojson数据

基础行政界线数据网站(可以下载):阿里云 数据可视化平台

加载在线资源需要安装axios: npm install axios

<script>
import "ol/ol.css";
import Map from "ol/Map";
import View from "ol/View";
import OSM from "ol/source/OSM";
import VectorSource from "ol/source/Vector";
import TileLayer from "ol/layer/Tile";
import VectorLayer from "ol/layer/Vector";
import { Style, Stroke } from "ol/style";
import * as Format from "ol/format";
import anhui from "../../../static/geojson/anhui.json";
export default {
  data() {
    return {
      map: null,
      view: null,
      layers: null,
      mapCenter: [117.35398140997644, 31.92075208871266],
      mapZoom: 7.9,
      boundarySource: null,
      boundaryLayer: null,
    };
  },
  mounted() {
    let osm = new TileLayer({
      source: new OSM(),
    });
    this.layers = [osm];
    this.view = new View({
      center: this.mapCenter,
      projection: "EPSG:4326",
      zoom: this.mapZoom,
    });
    this.map = new Map({
      target: "map",
      layers: this.layers,
      view: this.view,
    });
    
	//1.加载项目中geojson数据
    this.boundarySource = new VectorSource({
      features: new Format.GeoJSON().readFeatures(anhui),
    });
    this.boundaryLayer = new VectorLayer({
      source: this.boundarySource,
      style: new Style({
        stroke: new Stroke({//面的样式
          width: 3,
          color: "#0f9ce2",
        }),
        image: new Circle({//点的样式
          radius: 10,
          stroke: new Stroke({
            color: "#fff",
          }),
          fill: new Fill({
            color: "#3399CC",
          }),
        }),
      }),
    });
    this.map.addLayer(this.boundaryLayer);

	//2.加载在线资源
	//在线资源地址需要配置跨域代理
	axios.get("/map/data/anhui.json").then((res) => {
      //如上加载
    });
  },
};
</script>
//geojson数据格式如下
{
  "type": "GeometryCollection",
  "geometries": [
    {"type": "Point","coordinates": [117.2597825704957, 31.817722108030615],"properties": {"ID":1,"MC": "合肥"}},//点
    {"type":"Feature","geometry":{"type":"MultiPolygon","coordinates":[[[[117.032273,31.256607],...,[117.032273,31.256607]]]]}}//面
  ]
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Vue3中使用OpenLayers初次加载叠加图片图层,可以借助OpenLayersVue3的生命周期函数来完成。以下是一个简单的示例代码: ```vue <template> <div class="map"> <div id="map" class="map-container"></div> </div> </template> <script> import { onMounted } from 'vue'; import Map from 'ol/Map'; import View from 'ol/View'; import TileLayer from 'ol/layer/Tile'; import OSM from 'ol/source/OSM'; import ImageLayer from 'ol/layer/Image'; import ImageStatic from 'ol/source/ImageStatic'; import { fromLonLat } from 'ol/proj'; export default { name: 'MapView', setup() { let map; onMounted(() => { // 创建地图 map = new Map({ target: 'map', layers: [ new TileLayer({ source: new OSM() }) ], view: new View({ center: fromLonLat([116.39, 39.9]), zoom: 10 }) }); // 创建Image图层 const imageLayer = new ImageLayer({ source: new ImageStatic({ url: 'path/to/image.png', // 图片路径 imageSize: [width, height], // 图片大小 projection: map.getView().getProjection(), // 投影 imageExtent: [left, bottom, right, top] // 图片范围 }) }); map.addLayer(imageLayer); }); return { map }; } } </script> <style scoped> .map { width: 100%; height: 100%; } .map-container { width: 100%; height: 100%; } </style> ``` 在上面的示例中,我们使用了Vue3的`onMounted`生命周期函数来创建地图和图层。我们首先导入需要使用的OpenLayers模块,然后在`onMounted`函数中创建地图,并将OSM图层添加到地图中。接下来,我们创建了一个Image图层,并将其添加到地图中。我们使用ImageStatic源来指定要叠加的图片,需要提供图片的路径、大小、投影和范围。最后,我们将Image图层添加到地图中,这样图片就会显示在地图上了。 希望这个示例能对你有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值