Vue + OpenLayers Quick Start Tutorial

Vue + OpenLayers Quick Start Tutorial

Openlayers is a modular, high-performance and feature-rich JavaScript package for WebGIS clients. It is used to display and interact with maps and spatial data, and has a flexible extension mechanism.

In short, using Openlayers (hereinafter referred to as ol) can flexibly and freely display various maps and spatial data. And this framework is completely free and open source.

Preface

This article records the introduction to using OpenLayers in Vue, uses OpenLayers to create a map component, and uses the map provided by OpenLayers and local images as maps.

Overview
OpenLayers makes it easy to put a dynamic map in any web page. It can display map tiles, vector data and markers loaded from any source. OpenLayers has been developed to further the use of geographic information of all kinds. It is completely free, Open Source JavaScript, released under the 2-clause BSD License (also known as the FreeBSD).

Official address: https://openlayers.org/

1. Install the OpenLayers library

cnpm install ol

2. Create OpenLayers component in Vue

Rendering

insert image description here

Code

	<template>
	  <div id="map" class="map"></div>
	</template>
	
	<script>
	import "ol/ol.css";
	import Map from "ol/Map";
	import OSM from "ol/source/OSM";
	import TileLayer from "ol/layer/Tile";
	import View from "ol/View";
	
	export default {
	  mounted() {
	    this.initMap();
	  },
	  methods: {
	    initMap() {
	      new Map({
	        layers:
	          new TileLayer({
	            source: new OSM()
	          })
	        ],
	        target: "map",
	        view: new View({
	          center: [0, 0],
	          zoom: 2
	        })
	      });
	
	      console.log("init finished");
	    }
	  }
	};
	</script>
	<style>
	.map {
	  width: 100%;
	  height: 400px;
	}
	</style>

3. OpenLayers uses local images as maps

Effect picture:

Code

	<template>
	  <div>
	    <div id="map" class="map"></div>
	  </div>
	</template>
	
	<script>
	import "ol/ol.css";
	import ImageLayer from "ol/layer/Image";
	import Map from "ol/Map";
	import Projection from "ol/proj/Projection";
	import Static from "ol/source/ImageStatic";
	import View from "ol/View";
	import { getCenter } from "ol/extent";
	
	let extent = [0, 0, 338, 600];
	let projection = new Projection({
	  code: "xkcd-image",
	  units: "pixels",
	  extent: extent
	});
	
	export default {
	  data() {
	    return {
	      map: {}
	    };
	  },
	
	  mounted() {
	    this.initMap();
	  },
	
	  methods: {
	    initMap() {
	      this.map = new Map({
	        layers:
	          new ImageLayer({
	            source: new Static({
	              attributions: '© <a href="http://xkcd.com/license.html" rel="external nofollow" >xkcd</a>',
	              url: "http://localhost:8080/img/123.5cba1af6.jpg",
	              projection: projection,
	              imageExtent: extent
	            })
	          })
	        ],
	        target: "map",
	        view: new View({
	          projection: projection,
	          center: getCenter(extent),
	          zoom: 1,
	          maxZoom: 4,
	          minZoom: 1
	        })
	      });
	    }
	  }
	};
	</script>
	<style>
	.map {
	  width: 100%;
	  height: 400px;
	}
	</style>

This is the end of this article about the Vue + OpenLayers quick start learning tutorial. For more Vue OpenLayers introductory 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:
  • Vue uses openlayers to load Tiandi Map and Amap
  • Vue+Openlayers realizes real-time coordinate point display
  • Vue+openlayers draws provincial and municipal boundaries
  • Vue uses Tiandi Map and openlayers to realize the overlay display of multiple base maps

<<:  Use of Linux ln command

>>:  Some "pitfalls" of MySQL database upgrade

Recommend

The pitfall of MySQL numeric type auto-increment

When designing table structures, numeric types ar...

mysql5.7.14 decompressed version installation graphic tutorial

MySQL is divided into Community Edition (Communit...

Several techniques for playing sounds with CSS

CSS is the realm of style, layout, and presentati...

How to connect to MySQL database using Node-Red

To connect Node-red to the database (mysql), you ...

Implementation of docker-compose deployment of zk+kafka+storm cluster

Cluster Deployment Overview 172.22.12.20 172.22.1...

Implementation of Nginx Intranet Standalone Reverse Proxy

Table of contents 1 Nginx Installation 2 Configur...

Abbreviation of HTML DOCTYPE

If your DOCTYPE is as follows: Copy code The code ...

Tips for creating two-dimensional arrays in JavaScript

Creation of a two-dimensional array in Js: First ...

Detailed analysis of the blocking problem of js and css

Table of contents DOMContentLoaded and load What ...

What you need to know about responsive design

Responsive design is to perform corresponding ope...

Linux common commands chmod to modify file permissions 777 and 754

The following command is often used: chmod 777 文件...

jQuery implements clicking left and right buttons to switch pictures

This article example shares the specific code of ...

Detailed explanation on how to modify the default port of nginx

First find out where the configuration file is wh...

Summary of Binlog usage of MySQL database (must read)

I won't go into details about how important b...