简体   繁体   English

标记簇 - 传单 - 无法使其正常工作

[英]Marker Cluster - Leaflet - Can't get it working

So I am wanting to incorporate Marker Cluster into my map (obviously), however I am having a few issues. 所以我想将Marker Cluster合并到我的地图中(显然),但是我遇到了一些问题。 Note: I am doing this within a project and didn't write the code for the map itself, but I understand the gist of it. 注意:我在一个项目中这样做,并没有为地图本身编写代码,但我理解它的要点。

We fetch the locations of the markers from a geoJSON file that is stored in the database so we just reference the SQL query which then inserts that into the map (if that makes sense). 我们从存储在数据库中的geoJSON文件中获取标记的位置,因此我们只引用SQL查询,然后将其插入到地图中(如果这有意义)。

var geojsonFeature = <?php echo $trip['json_data'] ?>;

The full code is below - minus the SQL connection/query itself since it is irrelevant to what I am trying to achieve (well at least I think it is). 完整的代码在下面 - 减去SQL连接/查询本身,因为它与我想要实现的内容无关(至少我认为是这样)。

                                <!-- Create a Leaflet map with the appropriate options -->
                            var map = L.map('map', {
                                layers: []
                            }).setView([-33.85638, 151.2078], 11);

                            var geojsonFeature = <?php echo $trip['json_data']  ?>;

                            function onEachFeature(feature, layer) {

                                // Is this feature a Normal point or a destination
                                if (feature.properties && feature.properties.destination) {

                                    // This feature is a destination point
                                    var popString = "<b> Destination </b> <br>";
                                    popString += feature.properties.address;
                                    layer.bindPopup(popString);

                                } else {

                                    // This feature is a normal point
                                    var popString = "<b>" + feature.properties.name + " - " + feature.properties.group +"</b><br>";
                                    popString += feature.properties.address + "<br>";
                                    popString += "<b>Pickup: </b>" + feature.properties.pick_up_time + "<br>";
                                    popString += "<b>Estimated Group Cost: </b>$" + feature.properties.group_travel_cost;                                       
                                    layer.bindPopup(popString);
                                }
                            }

                            function setStyle(feature) {

                                if (feature.properties.destination) {
                                    console.log("Destination");
                                    // Customer icon for destination
                                } else {
                                    console.log("Origin");
                                }
                            }

                        <!-- Add the GeoJSON object to the GeoJSON layer -->

                            L.geoJson(geojsonFeature, { onEachFeature: onEachFeature, 
                                                        style: setStyle }).addTo(map);


                        <!-- Set the tile layer to the openstreemap tiles + extra properties -->
                            L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
                                maxZoom: 18,
                                attribution: 'Map data &copy; OpenStreetMap contributors'
                            }).addTo(map);

                        <!-- Cluster Markers -->

I've tried following the docs on the MarkerCluster Git, yet I'm still lost. 我已经尝试过关注MarkerCluster Git上的文档了,但我还是迷路了。

var markers = L.markerClusterGroup(); markers.addLayer(L.marker(getRandomLatLng(map))); ... Add more layers ... map.addLayer(markers);

Has anyone had any experience incorporating marker clusters? 有没有人有任何合并标记簇的经验? And if so, can someone lend a helping hand. 如果是这样,有人可以伸出援助之手。

Side Note: Yes I am including all the relevant files needed to run it; 附注:是的我包括运行它所需的所有相关文件; JS, MarkerCluster.js - I am getting all these from the CDN. JS,MarkerCluster.js - 我从CDN获得了所有这些。

EDIT So I took the advice given by Nathan below, and the following is what has occurred now. 编辑所以我接受了Nathan给出的建议,以下是现在发生的事情。 Below is my new code. 以下是我的新代码。

                                    <!-- Create a Leaflet map with the appropriate options -->

                       var map = L.map('map', {
                            layers: []
                        }).setView([-33.85638, 151.2078], 11);

                        var clusterGroup = L.markerClusterGroup();
                        var geojsonFeature = <?php echo $trip['json_data']  ?>;

                        function onEachFeature(feature, layer) {

                            // Is this feature a Normal point or a destination
                            if (feature.properties && feature.properties.destination) {

                                // This feature is a destination point
                                var popString = "<b> Destination </b> <br>";
                                popString += feature.properties.address;
                                layer.bindPopup(popString);

                            } else {

                                // This feature is a normal point
                                var popString = "<b>" + feature.properties.name + " - " + feature.properties.group +"</b><br>";
                                popString += feature.properties.address + "<br>";
                                popString += "<b>Pickup: </b>" + feature.properties.pick_up_time + "<br>";
                                popString += "<b>Estimated Group Cost: </b>$" + feature.properties.group_travel_cost;                                       
                                layer.bindPopup(popString);
                            }
                        }

                        function setStyle(feature) {

                            if (feature.properties.destination) {
                                console.log("Destination");
                                // Customer icon for destination
                            } else {
                                console.log("Origin");
                            }
                        }

                    <!-- Add the GeoJSON object to the GeoJSON layer -->
                    var geojsonLayer = L.geoJson(geojsonFeature,{onEachFeature:onEachFeature, style:setStyle});


                    <!-- Set the tile layer to the openstreemap tiles + extra properties -->
                        L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
                            maxZoom: 18,
                            attribution: 'Map data &copy; OpenStreetMap contributors'
                        }).addTo(map);

                    <!-- Cluster Markers -->
                    clusterGroup.addLayer(geojsonLayer);
                    map.addLayer(clusterGroup);

I added the var clusterGroup = L.markerClusterGroup(); 我添加了var clusterGroup = L.markerClusterGroup(); at the start. 在开始时。 I then replaced L.geoJson(geojsonFeature, { onEachFeature: onEachFeature, style: setStyle }).addTo(map); 然后我替换了L.geoJson(geojsonFeature, { onEachFeature: onEachFeature, style: setStyle }).addTo(map); with var geojsonLayer = L.geoJson(geojsonFeature,{onEachFeature:onEachFeature, style:setStyle}); with var geojsonLayer = L.geoJson(geojsonFeature,{onEachFeature:onEachFeature, style:setStyle}); And I then add clusterGroup.addLayer(geojsonLayer); map.addLayer(clusterGroup); 然后我添加clusterGroup.addLayer(geojsonLayer); map.addLayer(clusterGroup); clusterGroup.addLayer(geojsonLayer); map.addLayer(clusterGroup); after the tileLayer (if I do it anywhere before hand, the page does a never-ending loop before crashing). 在tileLayer之后(如果我在手边的任何地方执行它,页面会在崩溃之前执行永不停止的循环)。

The problem is, I get the clusters, however when I zoom into them, only markers outside of smaller clusters appear. 问题是,我得到了聚类,但是当我放大它们时,只显示较小聚类之外的标记。 Eg; 例如; if I went into a cluster of '5', no markers in that cluster are appearing. 如果我进入'5'的簇,则该簇中没有标记出现。

http://puu.sh/kQWoI/4ef5bb11fb.jpg As an example. http://puu.sh/kQWoI/4ef5bb11fb.jpg举个例子。

If you are getting valid GeoJSON from your query, you should be able to add it to a cluster pretty easily. 如果您从查询中获得有效的GeoJSON,则应该能够非常轻松地将其添加到群集中。 A more useful example to follow might be the one in the examples directory on GitHub: 一个更有用的示例可能是GitHub上examples目录中的一个:

https://github.com/Leaflet/Leaflet.markercluster/blob/master/example/geojson.html https://github.com/Leaflet/Leaflet.markercluster/blob/master/example/geojson.html

First, you set up a markerClusterGroup: 首先,设置markerClusterGroup:

var clusterGroup = L.markerClusterGroup();

Then you assign your geoJson layer to a named variable (but don't add it to the map): 然后将geoJson图层分配给命名变量(但不要将其添加到地图中):

var geojsonLayer = L.geoJson(geojsonFeature,{onEachFeature:onEachFeature, style:setStyle});

From there, you can add your layer to the cluster group and add that to the map: 从那里,您可以将图层添加到群集组并将其添加到地图:

clusterGroup.addLayer(geojsonLayer);
map.addLayer(clusterGroup);

It should be as simple as that. 它应该就这么简单。 Also make sure that you're referencing the css files as well as the js if you want to actually see icons for those clusters. 如果要实际查看这些群集的图标,还要确保引用css文件和js。

Here is a simple example fiddle that shows it working: 这是一个简单的示例小提示,显示它工作:

http://fiddle.jshell.net/nathansnider/tw5b0uuq/ http://fiddle.jshell.net/nathansnider/tw5b0uuq/

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

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