使用javascript和openlayers在网页中单击地图时获取鼠标位置

使用javascript和openlayers在网页中单击地图时获取鼠标位置,javascript,openlayers,Javascript,Openlayers,在openlayers.org网站上,有一个代码显示鼠标在地图上的位置。但它不会将位置存储在变量中供用户使用。我想有一个代码,当用户点击地图时,显示用户在窗口中的位置。 请帮帮我 我已经输入了openlayers.org提供的代码来显示鼠标的位置 <!DOCTYPE html> <html> <head> <title>Mouse Position</title> <link rel="stylesheet"

在openlayers.org网站上,有一个代码显示鼠标在地图上的位置。但它不会将位置存储在变量中供用户使用。我想有一个代码,当用户点击地图时,显示用户在窗口中的位置。 请帮帮我

我已经输入了openlayers.org提供的代码来显示鼠标的位置

<!DOCTYPE html>
<html>
  <head>
    <title>Mouse Position</title>
    <link rel="stylesheet" href="https://openlayers.org/en/v4.6.5/css/ol.css" type="text/css">
    <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
    <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
    <style>
      html, body {
        height: 100%;
        width: 100%;
    padding: 0px;
    margin: 0px;
      } 
      .map {
        height: 90%;
        width: 100%;
      }
    </style>
    <script src="https://openlayers.org/en/v4.6.5/build/ol.js"></script>
  </head>
  <body>
    <div id="map" class="map" "></div>

    <div id="mouse-position"></div>
    <form>
      <label>Projection </label>
      <select id="projection">
        <option value="EPSG:4326">EPSG:4326</option>
        <option value="EPSG:3857">EPSG:3857</option>
      </select>
      <label>Precision </label>
      <input id="precision" type="number" min="0" max="12" value="6"/>
    </form>
    <script>
      var mousePositionControl = new ol.control.MousePosition({
        coordinateFormat: ol.coordinate.createStringXY(6),
        projection: 'EPSG:4326',
        // comment the following two lines to have the mouse position
        // be placed within the map.
        className: 'custom-mouse-position',
        target: document.getElementById('mouse-position'),
        undefinedHTML: '&nbsp;'
      });

      var map = new ol.Map({
        controls: ol.control.defaults({
          attributionOptions: {
            collapsible: false
          }
        }).extend([mousePositionControl]),
        layers: [
          new ol.layer.Tile({
            source: new ol.source.OSM()
          })
        ],
        target: 'map',
        view: new ol.View({
          center: ol.proj.fromLonLat([51.39, 35.70]),
          zoom: 13
        })
      });

      var projectionSelect = document.getElementById('projection');
      projectionSelect.addEventListener('change', function(event) {
        mousePositionControl.setProjection(event.target.value);
      });

      var precisionInput = document.getElementById('precision');
      precisionInput.addEventListener('change', function(event) {
        var format = ol.coordinate.createStringXY(event.target.valueAsNumber);
        mousePositionControl.setCoordinateFormat(format);
      });


    </script>

</script> 
  </body>
</html>

鼠标位置
html,正文{
身高:100%;
宽度:100%;
填充:0px;
边际:0px;
} 
.地图{
身高:90%;
宽度:100%;
}

再见,谢谢。很好,再见,谢谢。很好。