openlayers学习笔记

三章-19-鼠标位置控件

2020-04-20  本文已影响0人  彩云飘过

本文基于腾讯课堂老胡的课《跟我学Openlayers--基础实例详解》做的学习笔记,使用的openlayers 5.3.x api。

源码 见 1019.html ,对应的 官网示例 https://openlayers.org/en/latest/examples/overviewmap.html?q=OverviewMap

image.png 效果图
<!DOCTYPE html>
<html>

<head>
  <title>鼠标位置控件
  </title>
  <link rel="stylesheet" href="../include/ol.css" type="text/css">
  <script src="../include/ol.js"></script>

</head>
<style>

</style>

<body>
  <div id="mouse-position"></div>
  <form>
    <label>投影 </label>
    <select id="projection">
      <option value="EPSG:4326">EPSG:4326</option>
      <option value="EPSG:3857">EPSG:3857</option>
    </select>
    <label>精度 </label>
    <input id="precision" type="number" min="0" max="12" value="4" />
  </form>
  <div id="map" class="map"></div>

  <script>
    var mousePositionControl = new ol.control.MousePosition({
      coordinateFormat: ol.coordinate.createStringXY(4),//坐标精度设置
      projection: 'EPSG:4326',//投影坐标系
      target: document.getElementById('mouse-position'), //绑定到自定义的DOM对象
     
    });

    var map = new ol.Map({
      controls: ol.control.defaults().extend([mousePositionControl]),
      layers: [
        new ol.layer.Tile({
          source: new ol.source.OSM()
        })
      ],
      target: 'map',
      view: new ol.View({
        center: [0, 0],
        zoom: 2
      })
    });

    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>
</body>

</html>
上一篇下一篇

猜你喜欢

热点阅读