如何将两个 geoJSON 特征集合添加到两个图层组中

编程入门 行业动态 更新时间:2024-10-27 16:36:33
本文介绍了如何将两个 geoJSON 特征集合添加到两个图层组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有两个 geoJSON 功能集合需要添加到地图中,我还希望通过图层可见性控制器打开和关闭它们,如 leafletjs/examples/layers-control.html

I have two geoJSON feature collections that I need to add to the map, and I also want them to be toggled on and off via the layer visibility controllers as shown in leafletjs/examples/layers-control.html

我该怎么做?

推荐答案

Leaflet的GeoJSON层L.GeoJSON的使用也有一个很好的教程,可以在这里找到:leafletjs/examples/geojson.html 这里是 的参考L.GeoJSON:leafletjs/reference.html#geojson您已经在 L.control.layers 上找到了教程,这里是它的参考:leafletjs/reference.html#control-layers

There is also a very good tutorial on the usage of L.GeoJSON, Leaflet's GeoJSON layer, which can be found here: leafletjs/examples/geojson.html and here is the reference for L.GeoJSON: leafletjs/reference.html#geojson You already found the tutorial on L.control.layers, here is the reference for it: leafletjs/reference.html#control-layers

其实做起来很简单,只需要创建一个layercontrol,使用你喜欢的XHR库加载一个GeoJSON文件到你的脚本中,使用检索到的数据来定义一个L.GeoJSON图层并将其添加到图层控件.在代码中:

It's actually quite simple to do, it's just a matter of creating a layercontrol, loading a GeoJSON file into your script by using your favorite XHR library, use the retrieved data to defined a L.GeoJSON layer and add it to the layercontrol. In code:

// Create the layercontrol and add it to the map var controlLayers = L.control.layers().addTo(map); // Loading a GeoJSON file (using jQuery's $.getJSON) $.getJSON('/my-folder/my-file.json', function (data) { // Use the data to create a GeoJSON layer and add it to the map var geojsonLayer = L.geoJson(data).addTo(map); // Add the geojson layer to the layercontrol controlLayers.addOverlay(geojsonLayer, 'My GeoJSON layer title'); });

关于 Plunker 的工作示例:plnkr.co/edit/tFVrrq?p=预览

A working example on Plunker: plnkr.co/edit/tFVrrq?p=preview

更多推荐

如何将两个 geoJSON 特征集合添加到两个图层组中

本文发布于:2023-10-26 17:09:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1530783.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两个   图层   如何将   组中   特征

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!