简体   繁体   English

OpenLayers:编辑坐标只需在 div 中单击以更改值(使地图居中)

[英]OpenLayers : Edit coordinates just by clicking in div to change the value (to center the map)

I woudlike to center my map just by click on my coordinates.我想通过单击我的坐标来使我的 map 居中。 This coordinates will be editable and after that change the value of center (view of my Map).该坐标将是可编辑的,然后更改中心的值(我的地图视图)。 I'm looking a way to do that:我正在寻找一种方法来做到这一点:

在此处输入图像描述

html: html:

<div id="mouse-position" class="mouse-position"></div>

js: js:

    var mouse_position = document.getElementById('mouse-position').addEventListener('click');
    var coord_centre;
    var mousePositionControl = new MousePosition({
      coordinateFormat: createStringXY(4),
      projection: 'EPSG:3857',
      // 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 Map({
      interactions: defaultInteractions().extend([dragAndDropInteraction]),
      layers: [
        new TileLayer({
          source: new XYZ({
            url: 'https://{a-c}.tile.openstreetmap.org/{z}/{x}/{y}.png'
          })
        }),
        coucheWMS
      ],
      target: 'map',
      controls: allcontrol({
        attributionOptions: false
      }).extend([mousePositionControl]),
      view: new View({
        center: coord_centre, // Value of mouse_position ? 
        zoom: 5
      })

You need to:你需要:

  1. take the user input接受用户输入
  2. parse it and if necessary convert to the required projection解析它,并在必要时转换为所需的投影
  3. use either view.setCenter(..) or view.animate(..) to move the map to that location.使用view.setCenter(..)view.animate(..)将 map 移动到该位置。

Using the control created by MousePosition will be quite tricky, as that control does not and was not intended to have an input.使用由MousePosition创建的控件将非常棘手,因为该控件没有也不打算有输入。 I suspect it will also be confusing to the user.我怀疑它也会让用户感到困惑。

Here is an example which uses a new input box:这是一个使用新输入框的示例:
https://stackblitz.com/edit/ol-center-on-entered-coordinates?file=index.js https://stackblitz.com/edit/ol-center-on-entered-coordinates?file=index.js
Enter a value ie: '45,1' then tab or enter, the map will change to that location.输入一个值,即:“45,1”,然后按 Tab 或 Enter,map 将更改为该位置。

If you really want to see the current coordinates and be able to enter coordinates in the same control, you will probably need to implement the mouse/center position detection yourself using map events, and then update the custom input with the relevant values.如果您真的想查看当前坐标并能够在同一控件中输入坐标,您可能需要使用 map 事件自己实现鼠标/中心 position 检测,然后使用相关值更新自定义输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM