简体   繁体   English

如何在 openlayers 上显示缩放级别(仅限 js/html 文件)?

[英]How to show zoom level on openlayers (js/html file only)?

looking for some help on this.寻求这方面的帮助。 I know it is something along the lines of我知道这是类似的东西

new OpenLayers.Control.ZoomStatus({
  autoActivate: true
})

Looking to add a div for the zoom status also: or what would be the best approach?还希望为缩放状态添加一个 div:或者最好的方法是什么?

If someone could offer some insight, it would be appreciated.如果有人可以提供一些见解,将不胜感激。 Couldn't find the answer in the documentation.在文档中找不到答案。

I don't believe in the current version of openlayers that this control has been implmented.我不相信在当前版本的 openlayers 中已经实现了这种控制。

The best way is to probably add your own element on your map to display the Zoom level and use最好的方法可能是在地图上添加您自己的元素以显示缩放级别并使用

map.getZoom()

To work out what the zoom level is to display it.计算出显示它的缩放级别。 Two ways you could approach it.你可以通过两种方式来处理它。 One ios to extend the OpenLayers.Control.Zoom class and add your own functions to the draw onZoomClick funtction.一个用于扩展 OpenLayers.Control.Zoom 类并将您自己的函数添加到绘制 onZoomClick 函数的 ios。 Then add your control to your map in place of the Normal OpenLayers.Control.zoom然后将您的控件添加到您的地图中,以代替 Normal OpenLayers.Control.zoom

The second would be to add an event to your map to capture a zoom event and update your element to display the zoom level there.第二种方法是向地图添加事件以捕获缩放事件并更新元素以在此处显示缩放级别。 To do this you would use the map.events.register() call on your map.为此,您可以在地图上使用 map.events.register() 调用。

The code to do the update should look something like this执行更新的代码应如下所示

 var zoomLevel = map.getZoom();
  document.getElementById('ZoomElement').innerHTML =  'Current Zoom: ' + zoomlevel + 'of ' + (map.numZoomLevels + 1);

Updating @Darkcylde for OL6.为 OL6 更新 @Darkcylde。 getZoom acts on View now. getZoom现在作用于 View。

And I added getting the zoom in tenths.我添加了十分之一的缩放。 And I just have the number showing, but that's a minor change我只显示了数字,但这是一个微小的变化

map.on('rendercomplete',function(e) {
  var zoomLevel = map.getView().getZoom();
  var zoomRounded = Math.round(zoomLevel*10)/10;
  document.getElementById('ZoomElement').innerHTML = zoomRounded;
})

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

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