2015-05-06 92 views
0

我正面临Leaflet(0.7.3版)的性能问题。我正在使用OSM贴图来显示一系列由装饰折线连接的CircleMarkers(每25px带有一个箭头图案)。加载需要一点时间,但主要问题是,当我缩放地图时,我开始面临严重滞后(来自缩放级别16),并超过一定的限制(大多数情况下说18),浏览器会冻结并最终崩溃(使用Chrome和Firefox进行测试)。我尝试了一堆1000个链接的标记,然后我下降到了大约100个,但仍然是同样的问题......当然,使用10个标记或更少的标记我没有任何问题。缩放时出现性能问题(小册子0.7.3)

您是否已经面临类似的麻烦?我如何优化传单表演,以便我可以使用超过100个链接的CircleMarkers进行精确缩放(超过16级)?我也想知道为什么演出都在不断下降得很厉害缩放时,同时标记量保持不变...

预先感谢您为您的回答,

Lenalys。

无法让PolylineDecorator插件在jsfiddle上工作。 但这里要说的是生成标记的代码:

地图初始化:

var map; 

function initializeMap(){ 
    "use strict"; 

    var layer; 
    var layer2; 

    function layerUrl(key, layer) { 
     return "http://wxs.ign.fr/" + key 
      + "/geoportail/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&" 
      + "LAYER=" + layer + "&STYLE=normal&TILEMATRIXSET=PM&" 
      + "TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&FORMAT=image%2Fjpeg"; 
    } 

    layer = L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', 
    { 
    attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors', 
    maxZoom: 18 
    }); 

    layer2 = L.tileLayer(
    layerUrl(IGN_AMBIENTE_KEY, "GEOGRAPHICALGRIDSYSTEMS.MAPS"), 
    {attribution: '&copy; <a href="http://www.ign.fr/">IGN</a>'} 
); 

    var baseMaps = { 
     "Terrestre": layer, 
     "Bathymetrique": layer2 
    }; 

    map = L.map('map', { 
     layers: [layer], 
     zoom: 8, 
     center: [42.152796, 9.139150], 
     zoomControl: false 
    }); 

    L.control.layers(baseMaps).addTo(map); 

    //add zoom control with your options 
    L.control.zoom({ 
     position:'topright' //topleft 
    }).addTo(map); 

    L.control.scale({ 
     position:'bottomleft', 
     imperial : false 
    }).addTo(map); 


} 

数据样本:

变种jsonData = { “12”:[{ “id_stm_device”: “7”,” individual_name “:” cerf3" , “纬度”: “42.657283333333”, “经度”: “9.42362”, “温度”:空, “脉冲”:NULL, “电池”: “20”, “DATE_TIME”:“2015 - 03-17 15:37:12“}, {”id_stm_device“:”7“,”individual_name“:”cerf3“,”纬度“:”42.657381666667“,”经度“:”9.42365“,”温度“:空,“pulse”:null,“battery”:“20”,“date_time”:“2015-03-17 16:42:16”}, {“id_stm_device”:“7”,“individual_name”:“cerf3”,“纬度”:“42.657381666667”,“经度”:“9.4236933333333”,“温度”:空,“脉冲”:空,“电池”: “20”,“date_time”:“2015-03-17 17:47:21”}, {“id_stm_device”:“7”,“individual_name”:“cerf3”,“latitude”:“42.657283333333”,“longitude “:”9.4237383333333“,”temperature“:null,”pulse“:null,”battery“:”20“,”date_time“:”2015-03-17 19:57:23“}], ”13“: [{ “id_stm_device”: “8”, “individual_name”: “cerf5”, “纬度”: “42.61683”, “经度”: “9.4804633333333”, “温度”: “17.45”, “脉冲”:NULL,“电池“:”80“,”date_time“:”2015-04-08 07:45:20“}, {”id_stm_device“:”8“,”individual_name“:”cerf5“,”纬度“:”42.538858333333“, “经度”:“9.48169”,“温度”:“14.37”,“脉冲”:空,“电池”:“80”,“日期时间”:“2015-04-08 08:00:29”}, { “id_stm_device”: “8”, “individual_name”: “cerf5”, “纬度”: “42.458748333333”, “经度”:“9 。“500225”,“temperature”:“14.46”,“pulse”:null,“battery”:“80”,“date_time”:“2015-04-08 08:15:49”}, {“id_stm_device”: “8”, “individual_name”: “cerf5”, “纬度”: “42.3302”, “经度”: “9.5374583333333”, “温度”: “15.19”, “脉冲”:空, “电池”: “80”, “date_time”:“2015-04-08 08:31:05”}, {“id_stm_device”:“8”,“individual_name”:“cerf5”,“纬度”:“42.170133333333”,“经度”:“9.5272116666667 “,”temperature“:”15.48“,”pulse“:null,”battery“:”80“,”date_time“:”2015-04-08 08:46:20“}, {”id_stm_device“:”8 “ ”individual_name“: ”cerf5“, ”纬度“: ”42.07959“, ”经度“: ”9.47688“, ”温度“: ”15.97“, ”脉冲“:NULL, ”电池“: ”80“,” DATE_TIME “:”2015-04-08 09:01:31“}, {”id_stm_device“:”8“,”individual_name“:”cerf5“,”纬度“:”42.076163333333“,”经度“:”9.4828633333333“, “温度”:“20.42”,“脉冲”:空,“电池”:“80”,“日期时间”:“2015-04-08 09:16:59”}, {“id_stm_device”:“8”,“individual_name”:“cerf5”,“纬度”:“42.07194”,“经度”:“9.4908866666667”,“温度”:“17.36”,“脉冲” “:”80“,”date_time“:”2015-04-08 09:32:17“}, {”id_stm_device“:”8“,”individual_name“:”cerf5“,”latitude“:”42.072583333333“, “经度”: “9.4901516666667”, “温度”:“17。36“,”pulse“:null,”battery“:”80“,”date_time“:”2015-04-08 09:47:32“}, {”id_stm_device“:”8“,”individual_name“:” cerf5" , “纬度”: “42.07238”, “经度”: “9.4904266666667”, “温度”: “19.38”, “脉冲”:NULL, “电池”: “80”, “DATE_TIME”:“2015-04- 08:10:42“}, {”id_stm_device“:”8“,”individual_name“:”cerf5“,”纬度“:”42.072298333333“,”经度“:”9.4904983333333“,”温度“:”17.46“ ,“pulse”:null,“battery”:“80”,“date_time”:“2015-04-08 10:17:55”}, {“id_stm_device”:“8”,“individual_name”:“cerf5” ,“纬度”:“42.095093333333”,“经度”:“9.5148383333333”,“温度”:“17.47”,“脉冲”:空,“电池”:“80”,“日期时间”:“2015-04-08 10 :33:12“}, {”id_stm_device“:”8“,”individual_name“:”cerf5“,”纬度“:”42.112881666667“,”经度“:”9.5133133333333“,”温度“:”19.3“脉冲“:空,”电池“:”80“,”date_time“:”2015-04-08 10:48:23“}, {”id_st m_device “:” 8" , “individual_name”: “cerf5”, “纬度”: “42.112875”, “经度”: “9.513285”, “温度”: “22.71”, “脉冲”:NULL, “电池”:” 80“,”date_time“:”2015-04-08 11:03:57“}, {”id_stm_device“:”8“,”individual_name“:”cerf5“,”纬度“:”42.141096666667“,”经度“ :“9.5078216666667”,“temperature”:“23.73”,“pulse”:null,“battery”:“80”,“date_time”:“2015-04-08 11:19:12”}, {“id_stm_device” : “8”, “individual_name”: “cerf5”, “纬度”: “42.282186666667”, “经度”: “9.5505183333333”, “温度”: “18.97”, “脉冲”:空, “电池”: “80” ,“date_time”:“2015-04-08 11:34:28”}, {“id_stm_device”:“8”,“individual_name”:“cerf5”,“纬度”:“42.405126666667”,“经度”:“ 9.531145“,”temperature“:”20.71“,”pulse“:null,”battery“:”80“,”date_time“:”2015-04-08 11:49:42“}, {”id_stm_device“:” 8" , “individual_name”: “cerf5”, “纬度”: “42.482063333333”, “经度”: “9.480665”, “温度”: “21.7”, “脉冲” :NULL, “电池”: “80”, “DATE_TIME”: “2015-04-09 12时05分07秒”}]}

var oJSON = JSON.parse(jsonData); 



var colors = [ 
    "#400080", 
    "#008000", 
    "#EC7600", 
    "#E40341", 
    "#0D5E5E", 
    "#919191", 
    "#FF3C9D", 
    "#A70A0E", 
    "#00BFBF", 
    "#7171FF" 
    ]; 

var classes = [ 
    "color1", 
    "color2", 
    "color3", 
    "color4", 
    "color5", 
    "color6", 
    "color7", 
    "color8", 
    "color9", 
    "color10" 
]; 


var lastMarkers = []; 
var layers = new Array(); 
var polyline; 
var decorator; 

window.graphicsDevices = []; 
var offsetLatitude = 0.003333; 
var offsetLongitude = 0.011666; 

标记instanciation:

function printGPSOnMap(oJSON){ 
    var nbKeys = 0; 

    for (var key in oJSON) { 
    nbKeys++; 

    var classe = classes[(key-1)%classes.length]; 
    var color = colors[(key-1)%colors.length]; 

    var positionInfo = []; 

    if (oJSON.hasOwnProperty(key)) { 

      var aInfo = oJSON[key]; 
      var marker; 
      var latlngs = Array(); 

      var startMarker = lastMarkers[key]; 
      if(startMarker !== undefined && startMarker != null) { 

      var myIcon = L.divIcon({className: "myCircle "+classe, iconSize : [ 20, 20 ] }); 
      startMarker.setIcon(myIcon); 
      latlngs.push(startMarker.getLatLng()); 
      } 
      for(var i = 0; i < aInfo.length; i++) { 
       var oInfos = aInfo[i]; 
       var sIdIndividual = oInfos["id_individual"]; 
       var sLongitude = oInfos["longitude"]; 
       var sLatitude = oInfos["latitude"]; 
       var sTemperature = oInfos["temperature"]; 
       var sPulse = oInfos["pulse"]; 
       var sBattery = oInfos["battery"]; 
       var sDatetime = oInfos["date_time"]; 
       var sIndividualName = oInfos["individual_name"]; 
       var id_device = oInfos["id_stm_device"]; 

       var popupMsg = "..."; 

       latlngs.push(L.marker([sLatitude,sLongitude]).getLatLng()); 

       marker = new MyCustomMarker([sLatitude,sLongitude], { 
             icon : L.divIcon({ 
              className : "myCircle "+classe + ((i == aInfo.length-1) ? ' myCircleEnd' : ''), 
              iconSize : [ 20, 20 ] 
             }) 
             }); 
             marker.bindPopup(popupMsg, { 
               showOnMouseOver: true 
             }); 
             marker.bindLabel(key, { 
               noHide: true, 
               direction: 'middle', 
               offset: [offset[0], offset[1]] 
             }); 
       positionInfo.push(marker); 
     } 

     lastMarkers[key] = marker; 
     } 

     if(latlngs.length > 1) 
     { 

     polyline = L.polyline(latlngs, {className: classe, weight: 2,opacity: 0.4}).addTo(map); 
     decorator = L.polylineDecorator(polyline, { 
     patterns: [ 
      // define a pattern of 10px-wide arrows, repeated every 20px on the line 
      {offset: 0, repeat: '25px', symbol: new L.Symbol.arrowHead({pixelSize: 10, pathOptions: {fillOpacity:   
       0.76, color: color, weight: 1}})} 
     ]}).addTo(map); 
    } 

    if(!window.graphicsDevices.hasOwnProperty(key)) 
     window.graphicsDevices[key] = []; 
    for(var i = 0; i < positionInfo.length; i++) { 
     window.graphicsDevices[key].push(positionInfo[i]); 
     positionInfo[i].addTo(map); 
     if(latlngs.length > 1){ 
      window.graphicsDevices[key].push(polyline); 
      polyline.addTo(map); 
      window.graphicsDevices[key].push(decorator); 
      decorator.addTo(map); 
     } 
     } 
    }//foreach key 

} 

代码为自定义标记:

var MyCustomMarker = L.Marker.extend({ 

    bindPopup: function(htmlContent, options) { 
    if (options && options.showOnMouseOver) { 
     // call the super method 
     L.Marker.prototype.bindPopup.apply(this, [htmlContent, options]); 
     // unbind the click event 
     this.off("click", this.openPopup, this); 
     // bind to mouse over 
     this.on("mouseover", function(e) { 
     // get the element that the mouse hovered onto 
     var target = e.originalEvent.fromElement || e.originalEvent.relatedTarget; 
     var parent = this._getParent(target, "leaflet-popup"); 
     // check to see if the element is a popup, and if it is this marker's popup 
     if (parent == this._popup._container) 
      return true; 
     // show the popup 
     this.openPopup(); 
     }, this); 
     // and mouse out 
     this.on("mouseout", function(e) { 
     // get the element that the mouse hovered onto 
     var target = e.originalEvent.toElement || e.originalEvent.relatedTarget; 
     // check to see if the element is a popup 
     if (this._getParent(target, "leaflet-popup")) { 
      L.DomEvent.on(this._popup._container, "mouseout", this._popupMouseOut, this); 
      return true; 
     } 
     // hide the popup 
     this.closePopup(); 
     }, this); 
    } 
    }, 
    _popupMouseOut: function(e) { 

    // detach the event 
    L.DomEvent.off(this._popup, "mouseout", this._popupMouseOut, this); 
    // get the element that the mouse hovered onto 
    var target = e.toElement || e.relatedTarget; 
    // check to see if the element is a popup 
    if (this._getParent(target, "leaflet-popup")) 
     return true; 
    // check to see if the marker was hovered back onto 
    if (target == this._icon) 
     return true; 
    // hide the popup 
    this.closePopup(); 
    }, 

    _getParent: function(element, className) { 

    var parent = null; 

    if(element != null) parent = element.parentNode; 

    while (parent != null) { 
     if (parent.className && L.DomUtil.hasClass(parent, className)) 
     return parent; 
     parent = parent.parentNode; 
    } 
    return false; 
    } 
}); 
+0

这听起来更像是有问题的代码的问题,而不是一般的Leaflet问题。是否有可能发布一些代码,理想情况下在jsfiddle或类似的? – Liedman

+0

我使用生成标记的代码编辑我的帖子:) – Lenalys

回答

0

您是否在canvas mode中衡量过性能? 在初始化传单地图容器之前使用L_PREFER_CANVAS = true.Might能够帮助您。

+0

我已经尝试了这个代码片段,尽管性能增益很小,但是在缩放之后,地图会继续滞后和freezzing。我使用自定义标记而不是circleMarker,也许问题可能来自它? – Lenalys

+0

我在我的帖子中添加了“MyCustomMarker”对象的代码。 – Lenalys

+0

您是否尝试过在标记之间使用非装饰的香草多段线而不是现在正在做的?尝试了解瓶颈在哪里。 – snkashis