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

Detailed explanation of MySQL master-slave inconsistency and solutions

1. MySQL master-slave asynchrony 1.1 Network Dela...

What is flex and a detailed tutorial on flex layout syntax

Flex Layout Flex is the abbreviation of Flexible ...

Analyze the duration of TIME_WAIT from the Linux source code

Table of contents 1. Introduction 2. First, let&#...

How to use the Linux more command in Linux common commands

more is one of our most commonly used tools. The ...

Simple implementation method of two-way data binding in js project

Table of contents Preface Publish-Subscriber Patt...

How to use Linux whatis command

01. Command Overview The whatis command searches ...

How to install golang under linux

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

Summary of CSS front-end knowledge points (must read)

1. The concept of css: (Cascading Style Sheet) Ad...

Python MySQL database table modification and query

Python connects to MySQL to modify and query data...

How to prompt and open hyperlink a

<br />The countless information on the Inter...

The use of anchor points in HTML_PowerNode Java Academy

Now let's summarize several situations of con...

Solution to the failure of entering the container due to full docker space

Since the problem occurred rather suddenly and th...

Drop-down menu implemented by HTML+CSS3+JS

Achieve results html <div class="containe...

Solution to the ineffective global style of the mini program custom component

Table of contents Too long to read Component styl...

Detailed steps for developing Java payment interface for Alipay

Table of contents first step Step 2 Step 3 Step 4...