Vue uses openlayers to load Tiandi Map and Amap

Vue uses openlayers to load Tiandi Map and Amap

1. World Map

1. Install openlayers in vue

 npm i --save ol

The vue mentioned here is built based on scaffolding. Create a new page, that is, the vue file, and configure the routing. Then I can directly put in my code and run it.

 Vue uses openlayers to load Tiandi Map and Amap <template>
  <div class="wrapper">
    <div>Map of the world</div>
    <div class="map" id="olMap"></div>
  </div>
</template>
<script>
import "ol/ol.css";
import {
  Tile as TileLayer } from "ol/layer";
import XYZ from "ol/source/XYZ";
import {
  defaults as defaultControls } from "ol/control";
import Map from "ol/Map.js";
import View from "ol/View.js";
export default {
  data() {
   return {
      map: null,
      parser: null,
    };
  },
  mounted() {
    this.initMap();
  },
  methods: {
    initMap() {
     const map = new Map({
       target: "olMap",
        view: new View({
 
          center: [0, 0], //Center point latitude and longitude zoom: 4, //Layer scaling projection: "EPSG:4326",
        }),
        controls: defaultControls({
          zoom: true,
          attribution: false,
          rotate: false,
        }),
      });
      this.map = map;
      // Add map let url = "http://t{0-7}.tianditu.com/DataServer?x={x}&y={y}&l={z}";
      url = `${
   url}&T=vec_c&tk=replace your key`;
      const source = new XYZ({
     url: url,
        projection: "EPSG:4326",
      });
      const tdtLayer = new TileLayer({
        source: source,
      });
      this.map.addLayer(tdtLayer);
      // Add annotation url = "http://t{0-7}.tianditu.com/DataServer?x={x}&y={y}&l={z}";
      url = `${
   url}&T=cva_c&tk=replace your key`;
      const sourceCVA = new XYZ({
        url: url,
        projection: "EPSG:4326",
      });
      const tdtcvaLayer = new TileLayer({
        source: sourceCVA,
      });
      this.map.addLayer(tdtcvaLayer);
    },
  },
};
</script>
<style scoped>
.map {
  width: 100%;
  height: 100vh;
}
</style>

The world map will be displayed.

Effect picture:

2. Amap

Compared with Tiandi Map, Amap is much easier. Just go to the code

 <template>
  <div class="wrapper">
    <div>Amap</div>
    <div id="map"></div>
  </div>
</template>
<script>
import {
 Map,View,Feature} from 'ol'

import * as olProj from 'ol/proj'
import {
 Point} from 'ol/geom'
import {
  Style, Fill, Stroke, Circle as sCircle } from "ol/style";
// Add layer import Tilelayer from 'ol/layer/Tile'
import {
 Vector as VectorLayer} from 'ol/layer'
import {
 XYZ,Vector as VectorSource} from 'ol/source'
//Import style file import "ol/ol.css"
export default {
  data() {
  return {
    map:null
    }
  },
  mounted() {
  this.init();
   this.setMarker();
  },
  methods: {
    init(){
       this.map = new Map({
         target:'map',
         layers:[new Tilelayer({
           source: new XYZ({
               url:'https://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}',
           })
         })
         ],
        view:new View({

            // Take Xi'an as the center of the map // The olProj.fromLonLat method converts longitude and latitude into corresponding coordinates center: olProj.fromLonLat([108.945951, 34.465262]),
          zoom:2
        }),
       })
    },
    setMarker(){
        let _style = new Style({
            image:new sCircle({
                radius:10,
                stroke:new Stroke({
                    color:"#fff",
                }),
                fill: new Fill({
                    color:'#3399CC',
                }),
            }),
        });
        let _feature = new Feature({
 
            geometry:new Point(olProj.fromLonLat([108.945951, 34.465262])),
        });
        _feature.setStyle(_style);
        let _marker = new VectorLayer({

            source: new VectorSource({
               feature:[_feature],
            }),
        });
        this.map.addLayer(_marker);
    },
  },
}
</script>
<style scoped>
  #map{
      /* Screen width and height */
    width: 100vw;
    height: 100vh;
  }
</style>

This is the end of this article about how Vue uses openlayers to load Tiandi Map and Amap. For more relevant content about openlayers loading Tiandi Map and Amap, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Vue uses Tiandi Map and openlayers to achieve multiple base map overlay display effects
  • vue-openlayers realizes the map coordinates pop-up box effect
  • Vue calls Gaode map example code
  • How to introduce Amap and its UI components into vue project
  • Vue Baidu Map + Detailed Explanation of Positioning
  • Vue Echarts implements a visual world map code example
  • Detailed explanation of how to call Baidu Map API in Vue project
  • Solve the problem of Vue dynamically loading local images
  • Two ways to configure Amap in Vue-Cli 3.0
  • Vue uses openlayers to create a map

<<:  Implementation of forced line breaks and non-line breaks in div, td, p and other containers in HTML

>>:  How to write the introduction content of the About page of the website

Recommend

Install and configure ssh in CentOS7

1. Install openssh-server yum install -y openssl ...

HTML Tutorial: HTML horizontal line segment

<br />This tag can display a horizontal line...

MySQL enables slow query (introduction to using EXPLAIN SQL statement)

Today, database operations are increasingly becom...

The difference between MySQL count(1), count(*), and count(field)

Table of contents 1. First look at COUNT 2. The d...

Django+vue registration and login sample code

register The front-end uses axios in vue to pass ...

How to view version information in Linux

How to view version information under Linux, incl...

CSS implements a pop-up window effect with a mask layer that can be closed

Pop-up windows are often used in actual developme...

Usage of Node.js http module

Table of contents Preface HTTP HTTP Server File S...

How to install golang under linux

Go is an open source programming language that ma...

Install Tomcat on Linux system and configure Service startup and shutdown

Configure service startup and shutdown in Linux s...

In-depth analysis of HTML table tags and related line break issues

What is a table? Table is an Html table, a carrie...

Graphic tutorial on installing CentOS7 on VMware 15.5

1. Create a new virtual machine in VMware 15.5 1....

Disadvantages and reasonable use of MySQL database index

Table of contents Proper use of indexes 1. Disadv...

Awk command line or script that helps you sort text files (recommended)

Awk is a powerful tool that can perform some task...

The most basic code for web pages

◆Add to favorites illustrate Click to add your we...