简体   繁体   English

Leaflet中具有动态数据的Choropleth映射

[英]Choropleth map with dynamically data in Leaflet

I am struggling with Javascript and Leaflet tryind to realize a dashboard. 我正在努力用Javascript和Leaflet tryind实现仪表板。 I am trying to realize a choropleth map following this tutorial . 我正在尝试按照本教程实现一个Choropleth贴图。 I need to change dynamically data display on map according to two select menu. 我需要根据两个选择菜单来动态更改地图上的数据显示。

Selects: 选择:

<select class="c-select" id="methodSelected" name="methodSelected" required>
</select>
<select class="c-select" id="yearSelected" name="yearSelected" required>
</select>

I am holding changing on selects with 我正在与改变选择

$("#methodSelected").change()
$("#yearSelected").change()

both are declared inside a 两者都在一个内部声明

$(document).ready(function() {}

In this block I also declare the required variables and functions 在此块中,我还声明了所需的变量和函数

var choroplethMap;
var q = d3_queue.queue();
q.defer(d3.json, "final2.geojson");
function ready(GeoJSON) {
    ...
    makePlaceHolderChoroplethMap();
}

makePlaceHolderChoroplethMap is like that makePlaceHolderChoroplethMap就像这样

function makePlaceHolderChoroplethMap() {
    choroplethMap = L.map('choroplethContainer').setView([51.505, -0.09], 2);
    L.tileLayer("url", 
       {
            id: id,
            attribution: attribution,
            accessToken: accessToken
        }
    ).addTo(choroplethMap);

Now when I will change values with the two select menu I want update map, so I put into $("#yearSelected").change() a call to makeChoroplethMap() . 现在,当我要使用两个选择菜单更改值时,我想更新地图,因此我$("#yearSelected").change()的调用放入$("#yearSelected").change() makeChoroplethMap()

function makeChoroplethMap() {}

Inside this function I put code following tutorial previously linked. 在此函数中,我将代码放在先前链接的教程后面。

The problem is that when I changed the values with that code I will re-add new layers over others previously added (Geojson, legend and control, as you can see) 问题是,当我用该代码更改值时,我将在以前添加的其他层(如您所见,Geojson,图例和控件)上重新添加新层。

在此处输入图片说明

So I tryed to leave only binding with data into makeChoroplethMap() , 所以我试图只将与数据的绑定留给makeChoroplethMap()

geojson = L.geoJson(data, 
    {
        style: style,
        onEachFeature: onEachFeature
    }
).addTo(choroplethMap);

but I recieve an error in info.addTo(choroplethMap); 但我收到info.addTo(choroplethMap);的错误info.addTo(choroplethMap); as t is undefined . 由于t is undefined I think because choroplethMap is not initialized. 我认为是因为choroplethMap没有初始化。 Map and select work, but controls are not displayed 'cause of error. 映射并选择工作,但未显示控件“由于错误。 For now (I think I breaked something because yesteday worked) also geojson layer overstay over country also if I change values with select (the previously one putted does not go off also I change values). 就目前而言(我认为我已经打破了某些东西,因为昨天起了作用),如果我使用select更改值,则geojson层也会在国家/地区超过停留时间(之前放置的一个也不会更改,我也更改了值)。

So my question is: how can re-bind data without re-adding also legend and other control? 所以我的问题是:如何在不重新添加图例和其他控件的情况下重新绑定数据?

Add the legend and the GeoJSON layer once , when you're initializing the map. 初始化地图时, 一次添加图例和GeoJSON图层。 If you add them in makeChoroplethMap , you'll be re- adding them. 如果将它们添加到makeChoroplethMapmakeChoroplethMap 重新添加它们。

Then, use L.GeoJSON.clearLayers() and the currently undocumented L.GeoJSON.addData() method to clear/add polygons. 然后,使用L.GeoJSON.clearLayers()当前未记录的L.GeoJSON.addData()方法清除/添加多边形。

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

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