简体   繁体   English

我可以使用JavaScript jQuery改善数千个元素的呈现方式吗?

[英]Can I improve the rendering of thousands of elements using JavaScript jQuery?

I am developing an application based on the Google Maps APIs: basically I have the postal codes of my country and I need to show them. 我正在开发基于Google Maps API的应用程序:基本上,我具有所在国家的邮政编码,需要显示它们。

Here is a video which shows my current implementation -> https://vid.me/S6PL 这是一个视频,显示了我当前的实现-> https://vid.me/S6PL

Postal codes on the map are created thanks to the google.maps.OverlayView class which I extend and create a custom PostalCodeOverlay (which is represented by the circles). 地图的邮政编码要归功于google.maps.OverlayView类,我对该类进行了扩展并创建了一个自定义的PostalCodeOverlay (以圆圈表示)。 Its draw method is the following: draw方法如下:

PostalCodeOverlay.prototype.draw = function() {

        var overlayProjection = this.getProjection();
        // Handle some positioning stuff here with some properties created before during construction
        var center = overlayProjection.fromLatLngToDivPixel(new google.maps.LatLng(this.centerLat, this.centerLng));
        var div = this.postalCodeOverlayDiv;
        div.style.position = 'absolute';
        div.style.left = (center.x - (width/2)) + 'px';
        div.style.top = (center.y - (height/2)) + 'px';
        div.style["-moz-border-radius"] = "50%";
        div.style["-webkit-border-radius"] = "50%";
        div.style["border-radius"] = "50%";
        // As I show it the first time with an animation and
        // the APIs call this method several times (e.g. when
        // the zoom level of the map changes), I have a custom
        // boolean property that tells me whether it's the first
        // time or not
        if (!this.justCreated) {        
            div.style.width = width + "px";
            div.style.height = height + "px";
        }
        else {
            this.justCreated = false;
            var $postalCodeOverlayDiv = $(div); // here I get a jQuery reference to the div which represents the overlay (the circle).
            $postalCodeOverlayDiv.css({
                "width": "0",
                "height": "0",
                "opacity": "0"
            });
            // Animate it a bit so that it looks nicer when it
            // is drawn the first time.
            $postalCodeOverlayDiv.animate({
                "opacity": "1",
                "width": width + "px",
                "height": height + "px"
            }, 500);

        }
};

Now, as you can see from the video, the circles are created smoothly when the zoom level is higher, cause the density of postal codes per viewport bounds is lower. 现在,从视频中可以看到,缩放级别越高,圆环就越平滑,导致每个视口边界的邮政编码密度降低。

But, when I start to zoom out, you can see that these nice effects are lost, as there are more postal codes per viewport bounds now (the area became wider). 但是,当我开始缩小图像时,您会发现这些漂亮的效果已经消失了,因为现在每个视口范围内的邮政编码越来越多(该区域变宽了)。

I fetch the postal codes from a database through AJAX (I cache them thanks to the Google Geocoding API locally on my DB). 我通过AJAX从数据库中获取邮政编码(由于在我的数据库中本地使用了Google Geocoding API,因此可以对它们进行缓存)。 The query which fetches them when the bounds are changed (I use map.addListener('bounds_changed', function() {...}) for that) is fast and returns as soon as a request is made. 更改边界时获取它们的查询( map.addListener('bounds_changed', function() {...})我使用map.addListener('bounds_changed', function() {...}) )非常快,并在发出请求后立即返回。 The returned JSON contains all the postal codes within the certain bounds searched, computed as the user moves within the map (as you can see from the video). 返回的JSON包含在搜索的特定范围内的所有邮政编码,这些邮政编码是随着用户在地图内移动而计算得出的(如您从视频中看到的那样)。 I then iterate over this JSON and for each entry (which represents the postal code), I create a new PostalCodeOverlay(...params...) with the relative information. 然后,我遍历此JSON,并为每个条目(代表邮政编码)创建一个带有相关信息的new PostalCodeOverlay(...params...)

The problem as you already understand is the rendering: when it comes to render thousands of elements the JavaScript engine lacks a bit and the map hangs. 您已经了解的问题是渲染:当渲染成千上万的元素时,JavaScript引擎缺乏了一点,地图挂起了。

I can post the code but I don't think it's needed here, I think it's enough to show you the video and the overlay I use (anyway, if something wasn't clear, please, tell me, I will update). 我可以发布代码,但我认为不需要在这里,我认为足以向您显示我使用的视频和叠加层(无论如何,如果不清楚,请告诉我,我会更新)。

Which is the best way to address such problems? 解决这些问题的最佳方法是什么? Is there a way to tell JavaScript/jQuery to handle the animations more smoothly when the underlying dataset is bigger? 当基础数据集更大时,是否有一种方法可以告诉JavaScript / jQuery更流畅地处理动画? Or maybe there are some Google Maps features I am unaware of and I can leverage them in the overlay I have created? 或者,也许我不了解某些Google Maps功能,可以在创建的叠加层中利用它们吗?

Anyway, some advice will be appreciated. 无论如何,一些建议将不胜感激。

Thanks for the attention. 感谢您的关注。

Google offers this document on Too Many Markers for rendering suggestions. Google在太多标记上提供了此文档,以提供建议。 There are a number of options to consider there. 那里有很多可供考虑的选项。

You want also want to get rid of the jQuery animation if there are hundreds of elements being drawn in the view. 如果在视图中绘制了数百个元素,您还希望摆脱jQuery动画。 jQuery animations involve thousands of drawing cycles on timers. jQuery动画涉及计时器上的数千个绘制周期。 Multiply that by hundreds or thousands of elements and it's more than a CPU can effectively keep up with. 将其乘以数百或数千个元素,就远远超过了CPU可以有效跟上的速度。 You could also experiment with using CSS animations instead of jQuery animations which have a possibility of being more efficient. 您还可以尝试使用CSS动画而不是jQuery动画,而jQuery动画可能会变得更有效率。

So, perhaps if the zoom level if past some threshold (thus making it likely that lost of postal codes are in the view), then skip the animation and just set the CSS property directly. 因此,如果缩放级别超过某个阈值(因此可能会使邮政编码丢失在视图中),则跳过动画并直接设置CSS属性。

Beyond that you could look into a more efficient way to draw your postal codes by perhaps removing the border-radius or simplifying some other CSS styles (you don't show everything involved in displaying that div so we can't make more specific recommendations). 除此之外,您可能会寻求一种更有效的方式来绘制邮政编码,方法是删除边框半径或简化某些其他CSS样式(您不会显示该div涉及的所有内容,因此我们无法提出更具体的建议) 。 Again, you could do this only when zoomed out beyond some threshold. 同样,您只有在缩小到某个阈值以上时才能执行此操作。

And, for situations where you're zoomed out a lot, you may want to skip rendering of heavily overlapped codes since they are all so small and so close together that they don't offer a whole lot of value anyway. 并且,对于放大很多的情况,您可能希望跳过重重叠的代码的渲染,因为它们是如此之小且如此紧密以至于它们始终无法提供很多价值。 The challenge here is to figure out how to do that while still saving overall CPU. 这里的挑战是弄清楚如何做到这一点,同时仍要节省总体CPU。 You could only show one postal code in a given screen segment or show no postal codes when the zoom out is beyond some threshold. 当缩小超出某个阈值时,您只能在给定的屏幕片段中显示一个邮政编码,或者不显示邮政编码。

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

相关问题 使用数千种动画元素提高CSS和JavaScript的性能 - Improve performance of css and javascript with thousands of animated elements 如何提高使用 Javascript 定位我的 html 元素的效率 - How can i improve the efficiency of targeting my html elements using Javascript jQuery建议:如何改进此功能以将元素滚动到视图中? - jQuery advice: How can I improve this function for scrolling elements into view? 如何改进此 Javascript,以便将事件附加到页面中的所有元素? - How can I improve this Javascript, for attaching events to all elements in page? 如何渲染数以万计的元素? - How can I render tens of thousands of elements? JQuery和Javascript:编写更好的代码-如何在两者上都进行改进 - JQuery and Javascript: Writing Better code - How can I improve on both 如何改进我的 Javascript/jQuery 逻辑? 包含示例 - How can I improve my Javascript/jQuery logic? Example included 如何改进具有等效 ID 的 jquery javascript 元素? - how to improve jquery javascript elements with equivalent id? 如何通过Javascript加速HTML处理,和/或提高动画的渲染速度? - How can I speed up HTML manipulation via Javascript, and/or improve rendering speed of animations? 如何通过在 Javascript 中使用 forEach 而不是 for 循环来改进我的逻辑 - How can I Improve my logic by using forEach, not for loop in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM