Method of dynamically loading geojson based on Vue+Openlayer

Method of dynamically loading geojson based on Vue+Openlayer

Load one or more features

<template>
  <div id="map" style="width: 100vw; height: 100vh"></div>
</template>
<script>
import "ol/ol.css";
import TileLayer from "ol/layer/Tile";
import VectorLayer from "ol/layer/Vector";
import VectorSource from "ol/source/Vector";
import XYZ from "ol/source/XYZ";
import { Map, View, Feature, ol } from "ol";
import { Style, Stroke, Fill } from "ol/style";
import { Polygon, MultiPolygon } from "ol/geom";
 
import areaGeo from "@/assets/chengdu.json";
 
export default {
  data() {
    return {
      map: {},
      areaLayer: {},
    };
  },
  mounted() {
    this.initMap(); //Initialize the map method this.addArea(areaGeo); //Add area layer method this.pointMove();
    this.getFeatureByClick();
  },
  methods: {
    pointMove() {
      // Set the style of the mouse over the vector element this.map.on("pointermove", (e) => {
        const isHover = this.map.hasFeatureAtPixel(e.pixel);
        this.map.getTargetElement().style.cursor = isHover ? "pointer" : "";
      });
    },
    getFeatureByClick() {
      this.map.on("click", (e) => {
        let features = this.map.getFeaturesAtPixel(e.pixel);
        this.map.getView().fit(features[0].getGeometry(), {
          duration: 1500,
          padding: [100, 100, 100, 100],
        });
      });
    },
    /**
     * Set area */
    addArea(geo = {}) {
      if (Object.keys(geo).length == 0 && geo.features.length == 0) return;
 
      // Set the layer this.areaLayer = new VectorLayer({
        source: new VectorSource({
          features: [],
        }),
      });
      //Add layer this.map.addLayer(this.areaLayer);
 
      let features = geo.features;
 
      for (let i in features) {
        let areaFeature = {};
 
        if (features[i].geometry.type == "MultiPolygon") {
          areaFeature = new Feature({
            geometry: new MultiPolygon(features[i].geometry.coordinates),
          });
        } else if (features[i].geometry.type == "Polygon") {
          areaFeature = new Feature({
            geometry: new Polygon(features[i].geometry.coordinates),
          });
        }
        areaFeature.setStyle(
          new Style({
            fill: new Fill({ color: "#4e98f444" }),
            stroke: new Stroke({
              width: 3,
              color: [71, 137, 227, 1],
            }),
          })
        );
        areaFeature.setProperties(features[i].properties);
        this.areaLayer.getSource().addFeature(areaFeature);
      }
    },
    /**
     * Initialize the map */
    initMap() {
      this.map = new Map({
        target: "map",
        layers:
          new TileLayer({
            source: new XYZ({
              url: "http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}",
            }),
          }),
        ],
        view: new View({
          projection: "EPSG:4326",
          center: [103, 31],
          zoom: 7,
        }),
      });
    },
  },
};
</script>

This is the end of this article about Vue+Openlayer dynamically loading geojson. For more relevant Vue Openlayer loading geojson content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Example code of using JsonView in Vue to display Json tree
  • Using jsonp for cross-domain request interface operation in vue
  • Vue3.0 method of loading json (non-ajax)
  • Using JSON in Vue to refresh the interface does not affect the countdown
  • Example of using jsonp plugin in vue
  • The two simplest ways to share Vue using JSON

<<:  Detailed explanation of non-primary key column overflow monitoring in MySQL tables

>>:  How to start a Java program in docker

Recommend

CentOS7 uses yum to install mysql 8.0.12

This article shares the detailed steps of install...

How to convert extra text into ellipsis in HTML

If you want to display extra text as ellipsis in ...

The process of building lamp architecture through docker container

Table of contents 1. Pull the centos image 2. Bui...

How to find websites with SQL injection (must read)

Method 1: Use Google advanced search, for example...

Introduction to several ways to introduce CSS in HTML

Table of contents 1. Embed CSS styles directly in...

Tutorial on migrating mysql from phpstudy to Linux

Project Purpose Migrate the data in MySQL 5.5.53 ...

Appreciation of the low-key and elegant web design in black, white and gray

Among classic color combinations, probably no one...

How to install SVN server under Linux

1. Yum installation yum install subversion 2. Con...

Detailed explanation of how to create MySql scheduled tasks in navicat

Detailed explanation of creating MySql scheduled ...

Solve the MySQL login 1045 problem under centos

Since the entire application needs to be deployed...

Detailed explanation of Vue's SSR server-side rendering example

Why use Server-Side Rendering (SSR) Better SEO, s...

Detailed explanation of the setting of background-image attribute in HTML

When it comes to pictures, the first thing we thi...

Detailed process of building nfs server using Docker's NFS-Ganesha image

Table of contents 1. Introduction to NFS-Ganesha ...

Linux debugging tools that developers and operators must look at [Recommended]

System performance expert Brendan D. Gregg update...