Polygon drawing not displaying on openlayers map

My task is to draw a polygon on an OpenLayers map. The code I am using for this purpose is as follows:

   draw = new Draw({
        source: this.vectorSource,
        type: 'Polygon'
    })
    draw.on('drawend', e => {

             // Solution 1, the result is not what I need
             let coords = e.feature.getGeometry().getCoordinates()

             // Solution 2 gives correct results, but the drawn polygon disappears
             let coords = e.feature.getGeometry().transform('EPSG:3857', 'EPSG:4326').getCoordinates()
    }
    this.olmap.addInteraction(draw)

I need to store the transformed coordinates in a database, but Solution #2 does not maintain the visibility of the drawn polygon. With Solution #1, it does not provide the required formatted coordinates if I try to transform them later using

    transform(coords, 'EPSG:3857', 'EPSG:4326')

Please advise me on how to correct the issue with maintaining the visibility of the polygon and obtaining the transformed coordinates.

Answer â„–1

In order to maintain the original geometry, it is essential to clone it beforehand.

let coordinates = element.feature.getGeometry().clone().transform('EPSG:3857', 'EPSG:4326').getCoordinates();

If you skip cloning the geometry, the feature may end up being positioned near point [0, 0] in view coordinates.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide to setting up Date Range Validator within MVC 4

Is there a way to limit the user from inputting a date outside of a specific range in my MVC 4 application? I'd appreciate any advice on how to achieve this. ...

Struggling to locate the element using xpath on a JavaScript enabled website in Selenium with Chrome

I have been attempting to extract data from a website that utilizes Javascript. Despite researching similar inquiries on xpath in Selenium, I have not found a solution. I initially tried using requests, but as the JavaScript doesn't load completely, I ...

The type 'function that takes in a CustomEvent and returns void' cannot be assigned to a parameter of type 'EventListenerOrEventListenerObject'

When I upgraded from TypeScript version 2.5.3 to 2.6.1, my custom event setup started giving me an error. window.addEventListener('OnRewards', (e: CustomEvent) => { // my code here }) [ts] Argument of type '(e: CustomEvent) => ...

Is there a way to identify the element causing an error in promise.map and then either log it to the console or skip it using code?

When running the code provided below, I encounter a 500 error for certain URLs in my list due to the structure of the page. The error occurs specifically on the line .map((htmlOnePage, index) => when some URLs lead to invalid pages, causing the prog ...

NodeJS API Language Configuration

I'm currently working on integrating the DuckDuckGo Instant Answer Api into my NodeJS application. To do so, I am making a data request from the API using Node Request. var request = require('request'); request('http://api.duckduckgo.c ...

Is it a Javascript comparison glitch, or have I overlooked something important?

Recently, I've been working on a JavaScript code that is designed to retrieve data from a server regarding the temperature readings from 2 sensors. The data is stored in a text file where each line includes a date along with 2 values corresponding to ...

Having difficulty retrieving data in mongodb using category id

Currently, I am attempting to sort and filter my pets based on categories. The pets are modeled as follows: const Pet = mongoose.model( 'Pet', new Schema({ name: { type: String, required: true, } ...

Show the current phone number with the default flag instead of choosing the appropriate one using the initial country flag in intl-tel-input

Utilizing intl-tel-input to store a user's full international number in the database has been successful. However, when attempting to display the phone number, it correctly removes the country code but does not select the appropriate country flag; ins ...

Explore within the attribute of a JSON

Here is the JSON structure: var parse = [ { "variation_id":"34", "attributes":{ "attribute_pa_rozmiar":"m" }, "image_src":"http://localhost/Fraise-WP/wp-content/uploads/2013/06/Jellyfish-300x300.jpg", "image_l ...

Creating a well-aligned form using Material-UI

Exploring Material-UI for the first time! How can a form be built where certain fields are arranged horizontally, others stacked vertically, and all aligned perfectly both vertically and horizontally? Check out this example image: https://i.sstatic.net/5R ...

Update the URL for the source of the Server-Sent event

Is there a way to modify the source set in the declaration of EventSource? I attempted this approach: var source = new EventSource("/blahblah.php?path=" + (window.location.pathname)); // A few lines below... source.url = "/blahblah.php?path=" + url; Nev ...

No response headers retrieved from WebAPI

Currently, I am utilizing the ASP.NET WebApi in conjunction with a ReactJs application on the front end. In this scenario, I am working on implementing a Get method that enables file downloads from the server. My objective is to configure both the Content- ...

The response from getStaticProps in Next.js is not valid

While following the Next.js documentation, I attempted to retrieve data from a local server but encountered an error message: FetchError: invalid json response body at http://localhost:3000/agency/all reason: Unexpected token < in JSON at position 0 ...

modify the b-form-checkbox component in a Vue application

I have inserted the <b-form-checkbox> element into a <b-table>: https://jsfiddle.net/fmattioni/L269rd7s/ This is my current objective: 1 - Initially, all checkboxes in the table are checked using checked="true". 2 - Try unchecking ...

When the buffer is sent through the socket in NodeJS, the console responds by stating that it is not recognized as a buffer

I've been exploring the implementation of AES encryption in ECB mode and here is the code snippet I'm working with. function encrypt (key, iv, plaintext) { if(algorithm == 'aes-128-ecb') iv = new Buffer(''); var ciphe ...

Utilizing numerous await statements within a single asynchronous function

My async function has 3 awaits, like this: const sequenceOfCalls = async(req, res, next) =>{ await mongoQuery(); await apiCall1(); await apiCall2(); } apiCall1 uses response of mongoQuery and apiCall2 uses response of apiCall1. The issue is ...

After initializing a Vue instance, the textContent of various HTML elements is obtained

Before and after creating a Vue instance over an element, different values of textContent are retrieved. Click here to see the issue in action To observe the varying outputs, check the console. The white space characters are being stripped off by the Vu ...

What could be the root cause behind the error encountered while trying to authenticate a JWT?

I've been working on integrating a Google OAuth login feature. Once the user successfully logs in with their Google account, a JWT token is sent to this endpoint on my Express server, where it is then decoded using jsonwebtoken: app.post('/login/ ...

Using local file paths in v-lazy-image does not function properly

When trying to use the v-lazy-image plugin for my page banner with local image files, it does not seem to work. <v-lazy-image srcset="../../../assets/images/banners/Small-Banner.svg 320w, ../../../assets/images/banners/Big-Banner.svg 480w&quo ...

ng-options do not refresh automatically when modifying elements in the array

Having trouble updating the data in a select list? It seems that when selecting 'test', the value retrieved from the API is 'ÅšlÄ…sk' even though it's not listed. For example: I select 'test' but it shows as 'ÅšlÄ ...