数百个标记同时移动.糟糕的表现

Hundreds of markers moving simultaneously. Bad performance

本文关键字:移动 百个      更新时间:2023-09-26

我使用的是谷歌地图api v3,我需要同时移动许多(数百)个标记。从服务器上,我得到了一组对象,表示一个标记随时间(几个月)移动时的坐标,并按移动时间排序。在我的地图上方,我有一个进度条,用"月:年"文本表示时间。我的代码所做的是在准确的时间移动标记(并绘制多段线),使其符合当前显示在"时间进度条"上的时间。在标记停止的每个位置,我都会留下一个新的标记来显示运动的历史。整个过程一次又一次地重复(setInterval()函数)。我的代码正在工作,问题是性能,所以我想问:有什么方法可以提高性能吗?有比我更好的方法吗?一定有什么。这是我的代码:

if (mapOverTime != null) {
    trackables.forEach(function (trackable) {
        var marker = new google.maps.Marker({
            map: mapOverTime,
            draggable: false,
            clickable: false,
            icon: geoIcon,
            animation: google.maps.Animation.none,                                
            zIndex: 2
        });            
        var polyline = new google.maps.Polyline(polyOptions);
        polyline.setMap(mapOverTime);
        // adding these to arrays so i can access and remove them from the map later
        markersArray.push(marker);
        polylineArray.push(polyline);
        trackable.years.forEach(function(year) {
            $.each(year.months, function(index, month) {
                setTimeout(function() {
                    if (month.coordinates.length > 0) {
                        var moveCounter = month.coordinates.length;
                        var timeToMove = Math.floor(pause_interval / moveCounter);
                        $.each(month.coordinates, function(i, position) {
                            var latTo = position.latitude;
                            var lngTo = position.longitude;
                            var destination = new google.maps.LatLng(latTo, lngTo);
                            setTimeout(function() {
                                moveToPosition(marker, destination, polyline);
                            }, (i) * timeToMove);
                        });
                    }
                }, pause_interval * index);
            });
        });
    });
}

这是使移动发生的功能:

function moveToPosition(marker, destination, polyline) {
  // current path of the polyline
  var path = polyline.getPath();
  // add new coordinate
  path.push(destination);
  // rendering whole line would make the map even more chaotic, this makes the line dissapear after 8 moves of the marker
  if (path.getLength() > 8) // comment out to render whole 
      th.removeAt(0); // path of polyline
  // set marker position to the new one
  marker.setPosition(destination);
  // render new marker on this position so that it is well visible where the coins were
  var historyPoint = new google.maps.Marker({
    map: mapOverTime,
    draggable: false,
    icon: historyIcon,
    animation: google.maps.Animation.none,
    zIndex: 0
  });
  historyPoint.setPosition(destination);
  // again add to global array so i can set access and remove this marker from the map later
  historyPoints.push(historyPoint);        
}

我所做的唯一一件事就是遍历所有标记及其坐标,在给定时间移动标记,在其后面划一条线,在新位置创建新标记,然后寻找另一个坐标。当1次迭代结束时,所有对象都将从映射中删除,并且过程将重新开始。知道如何提高性能吗?或者唯一的解决方案是减少我想要渲染的标记的数量?谢谢你的意见!

建议:

  • 为什么要指定animation: google.maps.Animation.none,?如果你不使用它,就删除它。根据文档,有两个值,BOUNCEDROP,所以无论如何都没有none值。

  • 如果可以的话,使用标准的javascript数组循环,而不是jquery的$.each();更快

  • 创建两个变量只是为了指定目的地坐标。你用过一次,再也不用了,为什么不使用原始值呢?即

    var latTo = position.latitude;
    var lngTo = position.longitude;
    var destination = new google.maps.LatLng(latTo, lngTo);
    

只是变成

    var destination = new google.maps.LatLng(position.latitude, position.longitude);
  • 你可以在这里做另一个改进。使用简写的内联结构表示法来指定目标坐标。它应该减少创建数百个LatLng对象的一些开销。即

    var destination = new google.maps.LatLng(position.latitude, position.longitude);
    

然后变为:

    var destination = {lat: position.latitude, lng: position.longitude};
  • 创建一条多段线,然后设置地图:

    var polyline = new google.maps.Polyline(polyOptions);
    polyline.setMap(mapOverTime);
    

我一直看到这种情况,但从来没有理解过…只需将map: mapOverTime添加到polyOptions中,就可以省去额外的函数调用。

  • 与historyPoint类似。。。不要对其调用setPosition(),只需在创建标记时传递的选项中指定position: destination

所有这些都是小事情,可能与尝试同时移动数百个标记没有什么不同,但我仍然建议你尝试它们。