【问题标题】:Leaflet.js and JSON data : optimization and performanceLeaflet.js 和 JSON 数据:优化和性能
【发布时间】:2013-10-05 07:45:58
【问题描述】:

我目前正在使用 JavaScript 构建客户数据的交互式地图。

到目前为止,我已经掌握了基础知识,但是当我开始使用标记超过 500 个 poi 或使用圆形标记超过 10,000 个时,性能开始下降......如果有人可以就如何优化我提供一些建议我已经得到了,或者也许我最好转移到像 mongo 这样的适当数据库来获取 json 数据,或者使用 Node Js 做工作服务器端?

任何建议将不胜感激:)

    var apiKey  = 'BC9A493B41014CAABB98F0471D759707',
          styleID = '108219';
    //    styleID = '997';


   // var map = L.map('map').setView([54.550, -4.433], 7);

      var southWest   = new L.LatLng(61.029031, 4.746094),
            northEast   = new L.LatLng(48.786962 ,-13.183594),
            bounds      = new L.LatLngBounds(southWest, northEast);

        var mapcenter      = new L.LatLng(53.457393,-2.900391);
        var map         = new L.Map('map',
                                {
                                    center: mapcenter,
                                    zoom: 7,
                                    // maxBounds: bounds,
                                    zoomControl: false
                                });

        var cloudmadeUrl = generateTileURL(apiKey, styleID),
            attribution = 'Map data © OpenStreetMap contributors.',
            tileLayer = new L.TileLayer(
                                cloudmadeUrl,
                                {
                                    maxZoom: 18,
                                    attribution: attribution,
                                });

            tileLayer.addTo(map);

        var zoomControl     = new L.Control.Zoom({ position: 'topleft'} );
            zoomControl.addTo(map);
        var scaleControl    = new L.Control.Scale({ position: 'bottomleft' });
            scaleControl.addTo(map);




      geojsonLayer = L.geoJson(geojson, {
          pointToLayer: function(feature, latlng) {
            return new L.CircleMarker(latlng, {fillColor: feature.properties.MarkerColour, fillOpacity: 0.5, stroke: false, radius: 6});
          // return new L.Marker(latlng, {icon: L.AwesomeMarkers.icon({icon: feature.properties.MarkerIcon, color: feature.properties.MarkerColour, iconColor: 'white'}) });
          },
        onEachFeature: function (feature, layer) {
            layer.bindPopup( '<strong><b>Customer Data</b></strong><br />' + '<b>Result : </b>' + feature.properties.Result + '<br />' + '<b>Postcode : </b>' + feature.properties.Postcode + '<br />' );
          }
      });

            console.log('starting: ' + window.performance.now());

      map.addLayer(geojsonLayer);

            console.log('ending: ' + window.performance.now());




    function generateTileURL(apiKey, styleID) {
        return 'http://{s}.tile.cloudmade.com/' + apiKey + '/' + styleID + '/256/{z}/{x}/{y}.png';
    }

和一些示例数据:

{
    "type": "Feature",
    "geometry": {
        "type": "Point",
         "coordinates": [
            -0.213467,
            51.494815
         ]
    },
    "properties": {
        "DateTime": "1372719435.39",
        "Result": "Cable Serviceable",
        "MarkerIcon": "ok-sign",
        "MarkerColour": "green",
        "Postcode": "W14 8UD"    
    }
},
{
    "type": "Feature",
    "geometry": {
        "type": "Point",
        "coordinates": [
            -0.389445,
            51.512121
        ]
    },
    "properties": {
        "DateTime": "1372719402.083",
        "Result": "Refer for National Serviceability",
        "MarkerIcon": "minus-sign",
        "MarkerColour": "red",
        "Postcode": "UB1 1NJ",

    }
 },
 {
    "type": "Feature",
    "geometry": {
        "type": "Point",
        "coordinates": [
            -0.411291,
            51.508012
        ]
    },
        "properties": {
        "DateTime": "1372719375.725",
        "Result": "Cable Serviceable",
        "MarkerIcon": "ok-sign",
        "MarkerColour": "green",
        "Postcode": "UB3 3JJ" 
     }
},
{
    "type": "Feature",
    "geometry": {
        "type": "Point",
        "coordinates": [
            -2.11054,
            53.500752
        ]
     },
    "properties": {
        "DateTime": "1372719299.088",
         "Result": "Cable Serviceable",
         "MarkerIcon": "ok-sign",
         "MarkerColour": "green",
         "Postcode": "OL7 9LR",

     }
 }

【问题讨论】:

  • 您的瓶颈不是显示该数据的数据库。在一个视图中显示 500pois 或 10,000 个圆形标记是真正的场景吗?或者您的问题是否意味着在包含 10000 个圆形标记的定义范围内的数据库中搜索?
  • 当前的演示版本正在处理大约 500 - 2500 的低值...最终版本需要能够同时显示 10,000 多个数据点...我相信这个问题是因为它在浏览器端实时渲染?只是想知道是否有其他方法可以真正解决扩展问题

标签: javascript json performance maps leaflet


【解决方案1】:

有几个Leaflet plugins 可以帮助处理在客户端浏览器中呈现大量点。

最简单的方法是使用一个插件来聚集标记,例如Marker Clusterer。 Clusterer 极大地帮助了客户端的渲染,因为这意味着客户端计算机不必绘制 10,000 个点,它只需绘制 10-40。

你也可以做一个热图 - 有两个插件,都基于 HTML5 Canvas:

【讨论】:

  • 我们正在使用markercluster插件,但仍然需要创建点。在 ipad 上仍然需要大约 10 秒才能获得渲染点。
  • 我认为你最好的选择是服务器端。您将在服务器上进行聚类,然后将聚类点作为单点发送到设备进行显示。这样一来,您就可以依靠服务器的强大功能来进行分析,而不是使用平板电脑或手机处理器。
猜你喜欢
  • 2013-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多