获取用户与地图交互时的实时OpenLayers地图数据。

huangapple go评论69阅读模式
英文:

Get openlayers map data live while user interacts with map

问题

I understand your request. Here's the translated code:

我正在使用OpenLayers(版本7.2)创建一个Web应用程序,我正在尝试找出如何实时输出当前中心、缩放和旋转。我找到了'movestart'和'moveend'事件,但这两者只在用户交互的开始或结束时触发一次。我正在寻找一种在用户交互时持续更新信息的方法。任何帮助将不胜感激。

这是我使用'moveend'的代码,是否有人可以帮助我在用户仍在拖动/重新定位地图时进行更新?

这是我的当前代码以及JSFiddle的链接。谢谢。

HTML

<div id="map" class="map"></div>
<div id="output">
    <h2>地图信息</h2>
    中心位置:<span id="center" class="info"></span><br>
    当前缩放:<span id="zoom" class="info"></span><br>
    当前旋转:<span id="rotation" class="info"></span>
</div>

CSS

#map{
    width: 300px;
    height: 300px;
}

#output{
    position: absolute;
    top: 8px;
    right: 8px;
    width: calc(100% - 340px);
    height: 150px;
    padding: 0 8px;
    background-color: lightgray;
    border: 1px dashed black;
}

#output h2{
    font-size: small;
    text-decoration:underline;
}

.info{
    font-size: smaller;
}

JS

var map = new ol.Map({
  layers: [
    new ol.layer.Tile({
      source: new ol.source.OSM()
    }),
    new ol.layer.Vector({
      source: new ol.source.Vector({
        url: 'data/geojson/countries.geojson',
        format: new ol.format.GeoJSON()
      })
    })
  ],
  target: 'map',
  
  view: new ol.View({
    center: ol.proj.fromLonLat([-63.5859487, 44.648618]), //哈利法克斯
    zoom: 14
  })
});

map.on("moveend", function() {
  var view = map.getView();
  var center = ol.proj.transform(view.getCenter(), 'EPSG:3857', 'EPSG:4326');
  var zoom = view.getZoom();
  var zoomInfo = '缩放级别 = ' + zoom;
  var rotation = view.getRotation();
  document.getElementById('center').innerHTML = center;
  document.getElementById('zoom').innerHTML = zoom;
  document.getElementById('rotation').innerHTML = rotation;
});

If you have any more code to be translated, please let me know.

英文:

I'm creating a webapp using openlayers (version 7.2) and I'm trying to figure out how to have a live output of the current center, zoom & rotation. I have found the 'movestart' and 'moveend' events however these both fire only once either at the beginning or end of the user interaction. I'm trying to find a way to continually update the information while the user is interacting. Any help would be appreciated.

Here is what I have using 'moveend' can anyone help me have this update while the user is still dragging/repositioning the map?

Here is my current code as well as alink to a JSFiddle.
Thanks.

HTML

&lt;div id=&quot;map&quot; class=&quot;map&quot;&gt;&lt;/div&gt;
&lt;div id=&quot;output&quot;&gt;
	&lt;h2&gt;Map Information&lt;/h2&gt;
	Center Position: &lt;span id=&quot;center&quot; class=&quot;info&quot;&gt;&lt;/span&gt;&lt;br&gt;
	Current Zoom: &lt;span id=&quot;zoom&quot; class=&quot;info&quot;&gt;&lt;/span&gt;&lt;br&gt;
	Current Rotation: &lt;span id=&quot;rotation&quot; class=&quot;info&quot;&gt;&lt;/span&gt;
&lt;/div&gt;





CSS

#map{
	width: 300px;
	height: 300px;
}

#output{
	position: absolute;
	top: 8px;
	right: 8px;
	width: calc(100% - 340px);
	height: 150px;
	padding: 0 8px;
	background-color: lightgray;
	border: 1px dashed black;
}

#output h2{
	font-size: small;
	text-decoration:underline;
}

.info{
	font-size: smaller;
}

JS

var map = new ol.Map({
  layers: [
    new ol.layer.Tile({
      source: new ol.source.OSM()
    }),
    new ol.layer.Vector({
      source: new ol.source.Vector({
        url: &#39;data/geojson/countries.geojson&#39;,
        format: new ol.format.GeoJSON()
      })
    })
  ],
  target: &#39;map&#39;,
  
  view: new ol.View({
    center: ol.proj.fromLonLat([-63.5859487, 44.648618]), //halifax
    zoom: 14
  })
});

map.on(&quot;moveend&quot;, function() {
  var view = map.getView();
  var center = ol.proj.transform(view.getCenter(), &#39;EPSG:3857&#39;, &#39;EPSG:4326&#39;);
  var zoom = view.getZoom();
  var zoomInfo = &#39;Zoom level = &#39; + zoom;
	var rotation = view.getRotation();
  document.getElementById(&#39;center&#39;).innerHTML = center;
  document.getElementById(&#39;zoom&#39;).innerHTML = zoom;
	document.getElementById(&#39;rotation&#39;).innerHTML = rotation;


});

https://jsfiddle.net/chudnovskym/d9cjzb03/21/

答案1

得分: 1

你可以在视图上使用更改事件

map.getView().on(['change:center', 'change:resolution', 'change:rotation'], function() {
  var view = map.getView();
  var center = ol.proj.transform(view.getCenter(), 'EPSG:3857', 'EPSG:4326');
  var zoom = view.getZoom();
  var zoomInfo = 'Zoom level = ' + zoom;
  var rotation = view.getRotation();
  document.getElementById('center').innerHTML = center;
  document.getElementById('zoom').innerHTML = zoom;
  document.getElementById('rotation').innerHTML = rotation;
});

https://jsfiddle.net/nvtf26h0/

英文:

You can use change events on the view

map.getView().on([&#39;change:center&#39;, &#39;change:resolution&#39;, &#39;change:rotation&#39;], function() {
  var view = map.getView();
  var center = ol.proj.transform(view.getCenter(), &#39;EPSG:3857&#39;, &#39;EPSG:4326&#39;);
  var zoom = view.getZoom();
  var zoomInfo = &#39;Zoom level = &#39; + zoom;
  var rotation = view.getRotation();
  document.getElementById(&#39;center&#39;).innerHTML = center;
  document.getElementById(&#39;zoom&#39;).innerHTML = zoom;
  document.getElementById(&#39;rotation&#39;).innerHTML = rotation;
});

https://jsfiddle.net/nvtf26h0/

huangapple
  • 本文由 发表于 2023年4月10日 19:55:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75976879.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定