简体   繁体   English

将标记链接到 leaflet 中的等值线多边形层

[英]linking markers to choropleth polygon layer in leaflet

I want to create an on-click event for choropleth layers to display markers within it's polygon coordinates.我想为 choropleth 图层创建一个单击事件,以在其多边形坐标中显示标记。 I've created the choropleth layer using the leaflet plug-in and am looking for a way to display the points within polygon - similar to how I've seen using turf-js plugin pointsWithinPolygon join - most of the examples I've seen online however link to creating a polygon with the points.我已经使用 leaflet 插件创建了 choropleth 层,并且正在寻找一种方法来显示多边形内的点 - 类似于我使用 turf-js 插件pointsWithinPolygon join 看到的方式 - 我在网上看到的大多数示例但是链接到用点创建多边形。 The two files I have (polygon attributes by county and point coords) have a like-property feature I could substitute for linkage (countyID) - however I am unsure of a dynamic method used to process the bind.我拥有的两个文件(按县和点坐标划分的多边形属性)具有类似属性的功能,我可以用链接(countyID)代替 - 但是我不确定用于处理绑定的动态方法。 Say - if I have 100+ counties within 1 state, linking the points to each county rather than manually creating the var featureCollection for each county and set of corresponding points.说 - 如果我在 1 state 内有 100 多个县,将这些点链接到每个县,而不是为每个县手动创建 var featureCollection 和一组对应的点。

Here is my choropleth code, the polygon coordinates for state output to bounds per county:这是我的等值线代码,state output 到每个县边界的多边形坐标:

let plethstate = new L.layerGroup()

geojson = L.choropleth(countydata, {
  valueProperty: "Amount", 
  scale: ["#ffffb2", "#b10026"],
  steps: 10,
  mode: "q",
  style: {
    color: "#fff",
    weight: 1,
    fillOpacity: 0.8
  },
  onEachFeature(feature, layer) {
    layer.bindPopup("CountyID: " + feature.properties.IDcounty + "<br>County: " + feature.properties.NAME + "<br># : " + feature.properties.Number);
    var state = feature.properties.IDcounty.slice(0,2);
    if (state == '51') layer.addTo(plethstate);
}
})

I'd like to reference this layer as a featureCollection using turf Js and find point markers within each county bounds so with an onclick() event of county, display markers within using my points layer:我想使用 turf Js 将此层引用为 featureCollection 并在每个县边界内找到点标记,以便使用县的 onclick() 事件,使用我的点层显示标记:

  var pts = L.geoJson('/data', {
    pointToLayer: function (feature){
      return new L.circleMarker([feature.geometry.coordinates[1], feature.geometry.coordinates[0]], {
      radius: getRadius(feature.properties.Amount),
      fillOpacity:0.8,
      color: getColor(feature.properties.Name),
  })
  }
})

On click function to bind polygon to points layer:单击 function 将多边形绑定到点层:

  $("#mybutton").click(function (event) { 
  var ptsWithin = turf.within(pts.toGeoJSON(), plethstate.toGeoJSON());
  console.log(ptsWithin)
  })

However, I may not be understanding the intrisic functionality of the turf-js method: Is it binding the points to var plethstate as a single layer?但是,我可能不理解 turf-js 方法的内在功能:它是否将点绑定到 var plethstate 作为单个层? Or does it inherently bind points to their correspondent polygon layer (each county) within the layergroup (after converting to geoJson)?或者它是否固有地将点绑定到图层组中它们对应的多边形图层(每个县)(在转换为 geoJson 之后)?

For anyone curious - I was able to bind markers to each polygon layer within the choropleth data file using this method:对于任何好奇的人 - 我能够使用这种方法将标记绑定到 choropleth 数据文件中的每个多边形层:

var group = L.featureGroup();
let plethstate = new L.layerGroup()

geojson = L.choropleth(countydata, {
  valueProperty: "Amount", 
  scale: ["#ffffb2", "#b10026"],
  steps: 10,
  mode: "q",
  style: {
    color: "#fff",
    weight: 1,
    fillOpacity: 0.8
  },
  onEachFeature(feature, layer) {
    layer.bindPopup("CountyID: " + feature.properties.IDcounty + "<br>County: " + feature.properties.NAME + "<br># : " + feature.properties.Number);
    layer.on("click", function (e){
          pointsInBuffer = L.geoJSON().addTo(group);
          var ptswithin = turf.pointsWithinPolygon(pts.toGeoJSON(), layer.toGeoJSON())
          console.log(ptswithin)
          pointsInBuffer.addData(ptswithin);
        })
    var state = feature.properties.IDcounty.slice(0,2);
    if (state == '51') layer.addTo(plethstate);
}
})

Note that the click function on the code above does not enable the markers but sends the data to the featureGroup.请注意,在上面的代码中单击 function 不会启用标记,而是将数据发送到 featureGroup。 To enable the featuregroup as an optional layer I've attached a checkbox (when checked render markers on click, and remove when unchecked).为了将特征组启用为可选层,我附加了一个复选框(选中时单击渲染标记,取消选中时删除)。 An optional button was added to refresh the markers in the feature group when clicked.添加了一个可选按钮以在单击时刷新要素组中的标记。

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

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