Three common uses of openlayers6 map overlay (popup window marker text)

Three common uses of openlayers6 map overlay (popup window marker text)

1. Write in front

Common map overlays are of these three types, such as: popup彈窗, label標注信息, text文本信息, etc.
The previous article talked about some properties, methods, events, etc. of overlay. This article mainly talks about the three most commonly used cases of overlay. For more information, please refer to the previous article openlayers6 [Eight] Detailed explanation of map overlay. These two articles will be related.
popup彈窗are a frequently encountered demand case, so let’s talk about them separately to make the map more vivid! ! !
You need to understand: overlay 然后通過map進行綁定,承載在頁面的dom 上的元素.

2. Overlay to implement popup window

2.1 Detailed explanation of addPopup() method of vue page

①: Create a new Overlay() instance and set related properties. Element is bound to the DOM of the outermost pop-up window of the page. ②: Add the overlay pop-up window to the page through map.addOverlay(this.overlay) . ③: Add an x ​​to closer.onclick event to close the pop-up window. ④: Click the map to trigger the pop-up window effect through this.map.on("singleclick", function(evt) event.

The specific code is as follows:

addPopup() {
    // Use variables to store the DOM object required for the popup window var container = document.getElementById("popup");
    var closer = document.getElementById("popup-closer");
    var content = document.getElementById("popup-content");

    // Create a pop-up Overlay object this.overlay = new Overlay({
        element: container, //Bind Overlay object and DOM object autoPan: true, //Define that the pop-up window may not be fully set when clicking on the edge autoPanAnimation: {
            duration: 250 //The animation time of the automatic pan effect is 9 milliseconds)
        }
    });
    // Add the pop-up window to the map this.map.addOverlay(this.overlay);

    let _that = this;
    /**
     * Add a function to respond to closing the pop-up window*/
    closer.onclick = function() {
        _that.overlay.setPosition(undefined);
        closer.blur();
        return false;
    };
    /**
     * Add a click map response function to handle pop-up actions*/
    this.map.on("singleclick", function(evt) {
        console.log(evt.coordinate);
        let coordinate = transform(
            evt.coordinate,
            "EPSG:3857",
            "EPSG:4326"
        );
        // Click on the ruler (here it is ruler (meters), not latitude and longitude);
        let hdms = toStringHDMS(toLonLat(evt.coordinate)); // Convert to longitude and latitude for display content.innerHTML = `
        <p>You clicked here:</p>
        <p>Latitude and longitude: <p><code> ${hdms} </code> <p>
        <p>Coordinates:</p>X: ${coordinate[0]} &nbsp;&nbsp; Y: ${coordinate[1]}`;
        _that.overlay.setPosition(evt.coordinate); //Display the overlay at the specified x,y coordinates});
}

Effect

insert image description here

2.2 Effect of autoPan property being false

Click on the far right side of the screen and you can see that the map will not adapt according to the mouse click position.

insert image description here

3. Overlay to implement label information

Vue Page

addMarker() {
    var marker = new Overlay({
        position: fromLonLat([104.043505, 30.58165]),
        positioning: "center-center",
        element: document.getElementById("marker"),
        stopEvent: false
    });
    this.map.addOverlay(marker);
},

insert image description here

4 Overlay implements text information

Vue Page

addText() {
    var textInfo = new Overlay({
        position: fromLonLat([104.043505, 30.58165]),
        offset: [20, -20],
        element: document.getElementById("textInfo")
    });
    this.map.addOverlay(textInfo);
},

insert image description here

5. Attach the complete code

<template>
    <div id="app">
        <div id="map" ref="map"></div>
        <div id="marker"></div>
        <div id="textInfo">I am text information</div>
        <div id="popup" class="ol-popup">
            <a href="#" rel="external nofollow" id="popup-closer" class="ol-popup-closer"></a>
            <div id="popup-content" class="popup-content"></div>
        </div>
    </div>
</template>

<script>
import "ol/ol.css";
import { Map, View, Coordinate } from "ol";
import { toStringHDMS } from "ol/coordinate";
import TileLayer from "ol/layer/Tile";
import XYZ from "ol/source/XYZ";
import Overlay from "ol/Overlay";
import { fromLonLat, transform, toLonLat } from "ol/proj";

// Pop-up window implementation export default {
    name: "dashboard",
    data() {
        return {
            map: null,
            overlay: null
        };
    },
    methods: {
        initMap() {
            let target = "map"; //Bind to the id of the page element for rendering let tileLayer = new TileLayer({
                source: new XYZ({
                    url:
                        "http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}"
                })
            });
            let view = new View({
                // projection: "EPSG:4326", //Use this coordinate system center: fromLonLat([104.912777, 34.730746]), //Map center coordinates zoom: 4.5 //Zoom level });
            this.map = new Map({
                target: target, //Bind DOM elements for rendering layers: [tileLayer], //Configure map data source view: view //Configure map display options (coordinate system, center point, zoom level, etc.)
            });
        },
        /**
         * The first type: point marker
         * Create a annotation information */
        addMarker() {
            var marker = new Overlay({
                position: fromLonLat([104.043505, 30.58165]),
                positioning: "center-center",
                element: document.getElementById("marker"),
                stopEvent: false
            });
            this.map.addOverlay(marker);
        },
        /**
         * The second type: text label label
         * Create a label annotation information */
        addText() {
            var textInfo = new Overlay({
                position: fromLonLat([104.043505, 30.58165]),
                offset: [20, -20],
                element: document.getElementById("textInfo")
            });
            this.map.addOverlay(textInfo);
        },
        /**
         * The third type: pop-up window
         * Create a pop-up message */
        addPopup() {
            // Use variables to store the DOM object required for the popup window var container = document.getElementById("popup");
            var closer = document.getElementById("popup-closer");
            var content = document.getElementById("popup-content");

            // Create a pop-up Overlay object this.overlay = new Overlay({
                element: container, //Bind Overlay object and DOM object autoPan: false, //Define that the pop-up window may not be fully set when clicking on the edge autoPanAnimation: {
                    duration: 250 //The animation time of the automatic pan effect is 9 milliseconds)
                }
            });
            // Add the pop-up window to the map this.map.addOverlay(this.overlay);

            let _that = this;
            /**
             * Add a function to respond to closing the pop-up window*/
            closer.onclick = function() {
                _that.overlay.setPosition(undefined);
                closer.blur();
                return false;
            };
            /**
             * Add a click response function to handle pop-up actions */
            this.map.on("singleclick", function(evt) {
                console.log(evt.coordinate);
                let coordinate = transform(
                    evt.coordinate,
                    "EPSG:3857",
                    "EPSG:4326"
                );
                // Click on the ruler (here it is ruler (meters), not latitude and longitude);
                let hdms = toStringHDMS(toLonLat(evt.coordinate)); // Convert to longitude and latitude for display content.innerHTML = `
                <p>You clicked here:</p>
                <p>Latitude and longitude: <p><code> ${hdms} </code> <p>
                <p>Coordinates:</p>X: ${coordinate[0]} &nbsp;&nbsp; Y: ${coordinate[1]}`;
                _that.overlay.setPosition(evt.coordinate); //Display the overlay at the specified x,y coordinates});
        }
    },
    mounted() {
        this.initMap();
        // Initialize pop-up window method this.addText();
        this.addMarker();
        this.addPopup();
    }
};
</script>
<style lang="scss" scoped>
html,
body {
    height: 100%;
}
#app {
    min-height: calc(100vh - 50px);
    width: 100%;
    position: relative;
    overflow: none;
    #map {
        height: 888px;
        min-height: calc(100vh - 50px);
    }
}
.ol-popup {
    position: absolute;
    background-color: white;
    -webkit-filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2));
    filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2));
    padding: 15px;
    border-radius: 10px;
    border: 1px solid #cccccc;
    bottom: 12px;
    left: -50px;
}
.ol-popup:after,
.ol-popup:before {
    top: 100%;
    border: solid transparent;
    content: " ";
    height: 0;
    width: 0;
    position: absolute;
    pointer-events: none;
}
.ol-popup:after {
    border-top-color: white;
    border-width: 10px;
    left: 48px;
    margin-left: -10px;
}
.ol-popup:before {
    border-top-color: #cccccc;
    border-width: 11px;
    left: 48px;
    margin-left: -11px;
}
.ol-popup-closer {
    text-decoration: none;
    position: absolute;
    top: 2px;
    right: 8px;
}
.popup-content {
    width: 400px;
}
.ol-popup-closer:after {
    content: "✖";
}
#marker {
    width: 20px;
    height: 20px;
    background: red;
    border-radius: 50%;
}
#textInfo {
    width: 200px;
    height: 40px;
    line-height: 40px;
    background: burlywood;
    color: yellow;
    text-align: center;
    font-size: 20px;
}
</style>

This is the end of this article about the three common uses of openlayers6 map overlay (popup window marker text). For more relevant vue openlayer popup map overlay 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:
  • Detailed explanation of map overlay in openlayers6
  • Openlayers draws map annotations
  • Solve the problem of OpenLayers 3 loading vector map source

<<:  Use of Linux usermod command

>>:  Complete steps to implement location punch-in using MySQL spatial functions

Recommend

Implementation of Nginx+ModSecurity security module deployment

Table of contents 1. Download 2. Deployment 1.Ngi...

Two query methods when the MySQL query field type is json

The table structure is as follows: id varchar(32)...

Tutorial on deploying the open source project Tcloud with Docker on CentOS8

1. Install Docker 1. I installed Centos7 in the v...

Vue opens a new window and implements a graphic example of parameter transfer

The function I want to achieve is to open a new w...

Research on the Input Button Function of Type File

<br />When uploading on some websites, a [Se...

Detailed discussion of memory and variable storage in JS

Table of contents Preface JS Magic Number Storing...

Win10 + Ubuntu20.04 LTS dual system boot interface beautification

Effect display The built-in boot interface is too...

What are the drawbacks of deploying the database in a Docker container?

Preface Docker has been very popular in the past ...

Overview of the basic components of HTML web pages

<br />The information on web pages is mainly...

MySQL Optimization: InnoDB Optimization

Study plans are easily interrupted and difficult ...

Modification of the default source sources.list file of ubuntu20.04 LTS system

If you accidentally modify the source.list conten...

Implementation of Vue3 style CSS variable injection

Table of contents summary Basic Example motivatio...