How to make visible long lat coordinates after click to map and make visible until next click?

Davidcz :

from this code for make visible long lat coordinate after moving the mouse over the map. And I rewrite to HTML page with newest OpenLayers version.

I would like to show this long lat coordinates after click to some place in map, not after move mouse on map as in the code. And make visible last coordinate until someone click to next place, nothing. I don't want the coordinates to disappear right away as in code.

<!doctype html>
<html lang="en">
  <head>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.3.0/css/ol.css" type="text/css">
    <style>
      .map {
        height: 400px;
        width: 100%;
      }
    </style>
    <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.3.0/build/ol.js"></script>
    <title>OpenLayers example</title>
  </head>
  <body>
  <div id="myposition"></div>
    <h2>My Map</h2>
    <div id="map" class="map"></div>
    <script type="text/javascript">
      var osm_default = new ol.layer.Tile({
        source: new ol.source.OSM()
      });

      var map = new ol.Map({
        layers: [osm_default],
        target: 'map',
        view: new ol.View({
          center: ol.proj.transform([-1.81185, 52.443141], 'EPSG:4326', 'EPSG:3857'),
          zoom: 6
        })
      });

      var mousePosition = new ol.control.MousePosition({
        coordinateFormat: ol.coordinate.createStringXY(2),
        projection: 'EPSG:4326',
        target: document.getElementById('myposition'),
        innerHTML: '&nbsp;'
      });

      map.addControl(mousePosition);
    </script>
  </body>
</html>
pzaenger :

Use singleclick or click event on your map:

map.on('singleclick', event => { 
  const coordinate = ol.proj.toLonLat(event.coordinate);
  const innerText = `Lon: ${coordinate[0].toFixed(4)}, Lat: ${coordinate[1].toFixed(4)}`;

  document.getElementById('myposition').innerText = innerText;
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I make a text box in a form not visible until after I click compute in C#?

How to make table visible, only after button click?

How to make visible the click action on Button?

How to make views visible per button click

how make a picture box visible with a button click

How make something after long click?

Alpine.js: How to make ref element visible and focus on click?

How to make checkboxes visible in RecyclerView on a button click from activity?

How to hide paragraph and make visible with heading click using Javascript

I have to click twice to make the div visible. How to solve?

Jquery script - Make Div visible on button click

On item click, make other items visible

How to make toast visible for long time?

How to make map areas visible with Firefox devtools?

How keep dropdown bootstrap visible until click back the button

How to make something visible after hiding it with SetActive

How to make a image visible after completion of Thread?

Label that wraps an input - how to make focus style visible only when focused using the keyboard, not mouse click?

Make HTML map element visible

How can I make Selenium click on the "Next" button until it is no longer possible?

How to make previous and next items visible using PageView in Flutter?

How to make next/previous paragraph visible inside in a div?

make a textbox visible on a tr(row) click and print the same of the data on button click in a DIV

how to make map lines visible over data in cartopy?

Making a Text Widget visible after button click

Click button after it is visible in Selenium using python

How to make the buttons visible in the alertController?

how to make keyboard not visible for UItextfield

How to make a UIView visible in ios