简体   繁体   English

在 leaflet map 上添加带有选定覆盖层名称的文本框

[英]Add text box on leaflet map with selected overlay layer name

Relatively new JavaScript user here, first question.相对较新的 JavaScript 用户,第一个问题。

So I have a choropleth leaflet map that uses a jQuery slider (via https://github.com/dwilhelm89/LeafletSlider ) to shift between years.所以我有一个 choropleth leaflet map 使用 jQuery slider (通过https://github.com/dwilhelm89/LeafletSlider )在年份之间移动。 The map contains about 50 years of global data, with each overlay layer corresponding to a layergroup containing each country's data for the appropriate year. map 包含大约 50 年的全球数据,每个叠加图层对应一个图层组,图层组包含每个国家/地区相应年份的数据。

The purpose of the slider is to allow the user to quickly shift between years. slider 的目的是允许用户在年份之间快速切换。 However, I would like a visual cue to let the user know what year is being displayed at any moment.但是,我想要一个视觉提示,让用户随时知道显示的是哪一年。 Is it possible to display something like a text box on the map that displays the name of the current overlay layer and automatically updates whenever the overlay layer switches?能不能在map上显示一个类似文本框的东西,显示当前覆盖层的名称,并在覆盖层切换时自动更新? (the name of each layergroup is its respective year) (每个图层组的名称是其各自的年份)

I know the textbox part is certainly possible ( Overlaying a text box on a leaflet.js map ), but I'm not sure how to dynamically update it with the necessary info.我知道文本框部分当然是可能的( 在 leaflet.js map 上覆盖一个文本框),但我不确定如何使用必要的信息动态更新它。

Thanks.谢谢。 Let me know if you need my code and I'll post it.让我知道您是否需要我的代码,我会发布它。

I've used Leaflet, Mapbox, and Google Maps on some personal and commercial projects, and whenever I wanted to overlay some information, I'd just use simple HTML elements.我在一些个人和商业项目中使用了 Leaflet、Mapbox 和 Google 地图,每当我想叠加一些信息时,我只会使用简单的 HTML 元素。 All you need to do is render whatever elements you want on the screen the same way you would normally, just ensure that you use correct position and applicable positioning units and ensure you have a higher z-index on your element that you want to show, ie your year indicator, than you do on your map element.您需要做的就是以与通常相同的方式在屏幕上呈现您想要的任何元素,只需确保您使用正确position和适用的定位单位,并确保您要显示的元素具有更高的z-index ,即你的年份指标,比你在 map 元素上做的要好。 Treat it just like you would any other HTML!就像对待任何其他 HTML 一样对待它!

Edit:编辑:

Here is an example screenshot: https://imgur.com/a/2fXf5CI .这是一个示例屏幕截图: https://imgur.com/a/2fXf5CI Also, if you aren't already using a position property on your Leaflet map, you should go ahead and add a position: relative;此外,如果您尚未在 Leaflet map 上使用position属性,则应在前面添加 go 并添加position: relative; property to the selector for the map so that you can also assign it a z-index .属性添加到 map 的选择器,以便您还可以为其分配一个z-index And then, in your year indicator's styles, give it a higher z-index value than the one you gave to your Leaflet map.然后,在你的年份指标 styles 中,给它一个比你给你的 Leaflet map 更高的z-index值。

Okay, I thought a bit and here's a quick solution.好吧,我想了一下,这是一个快速的解决方案。

 var sliderControl = null; var map = L.map("map").setView([51.95, 7.6], 9); L.tileLayer("//{s}.tile.osm.org/{z}/{x}/{y}.png", { attribution: '&copy; <a href="https://osm.org/copyright">OpenStreetMap</a> contributors', }).addTo(map); //Fetch some data from a GeoJSON file $.getJSON( "https://dwilhelm89.github.io/LeafletSlider/points.json", function (json) { var testlayer = L.geoJson(json); var sliderControl = L.control.sliderControl({ position: "topright", layer: testlayer, range: true, }); //Make sure to add the slider to the map;-) map.addControl(sliderControl); //An initialize the slider sliderControl.startSlider(); } ); map.on("layeradd", function () { map.eachLayer(function (layer) { if (layer instanceof L.Marker) { let desc = document.querySelector(".description"); // desc.textContent = JSON.stringify(layer.getLatLng()); desc.textContent = layer.feature.properties.time; } }); }); // create legend const legend = L.control({ position: "bottomleft" }); legend.onAdd = function () { let div = L.DomUtil.create("div", "description"); div.className = "description"; return div; }; legend.addTo(map);
 *, :after, :before { box-sizing: border-box; padding: 0; margin: 0; } html { height: 100%; } body, html, #map { width: 100%; height: 100%; margin: 0; padding: 0; }.description { border: 1px solid black; background: #fff; }
 <link rel="stylesheet" href="https://unpkg.com/leaflet@1.6.0/dist/leaflet.css" /> <link rel="stylesheet" href="https://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" type="text/css"> <script src="https://unpkg.com/leaflet@1.6.0/dist/leaflet.js"></script> <script src="https://code.jquery.com/jquery-1.9.1.min.js"></script> <script src="https://code.jquery.com/ui/1.9.2/jquery-ui.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui-touch-punch/0.2.2/jquery.ui.touch-punch.min.js"></script> <script src="https://rawgit.com/dwilhelm89/LeafletSlider/master/SliderControl.js" type="text/javascript"></script> <div id="map"></div>

And I recommend using the newer version of this plugin;) There you have the method event And the easier way to download the data about the marker.我建议使用此插件的较新版本;)那里有方法事件和下载有关标记的数据的更简单方法。

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

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