简体   繁体   English

添加标记时,谷歌地图js api会阻止ui

[英]google maps js api blocks ui when adding markers

I have a problem with google maps on mobile browsers. 我在移动浏览器上的Google地图有问题。

The project I'm working on requires about 500 markers to be shown on a map, but when adding them, the ui freezes. 我正在处理的项目要求在地图上显示大约500个标记,但是添加它们时,ui会冻结。

I'm using markerclusterer to add markers, and performance is fine once the markers are on the page. 我正在使用markerclusterer添加标记,并且一旦标记在页面上,性能就会很好。 Is there a way to add the markers asynchronously, or to pause adding markers once the user starts scrolling? 有没有一种方法可以异步添加标记,或者一旦用户开始滚动就暂停添加标记?

This is the code we're using: 这是我们正在使用的代码:

var mcOptions = { gridSize: 50, maxZoom: 15, zoomOnClick: false };
var mc = new MarkerClusterer(context.map, [], mcOptions);

getMarkerLocations().then(function(branches){    
    branches.forEach(function(branch) => {
        var marker = new google.maps.Marker({
            icon: {
                path: google.maps.SymbolPath.CIRCLE,
                scale: 2,
                strokeColor: 'red',
                fillColor: 'red',
                strokeWeight: 2,
                fillOpacity: 1
            },
            position: new google.maps.LatLng(branch.Latitude, branch.Longitude),
            visible: true
        });
        mc.addMarker(marker);
    });
});

Thank you geocodezip, that worked like a charm! 谢谢geocodezip,它的魅力十足! For anyone else with the same problem, here's how I fixed it: 对于其他有相同问题的人,这是我的解决方法:

.then(function(branches){
    var loopFunction = function(branchesToAdd) => {
                if (branchesToAdd.length === 0) return;
            var item = branchesToAdd.pop();
            var marker = new google.maps.Marker({
                ....
            });
            mc.addMarker(marker);
            setTimeout(function(){loopFunction(branchesToAdd)}, 30);
        };

    loopFunction(branchesToShow);
});

异步添加标记,使用setTimeout安排每个标记的添加,这将使浏览器有一些时间来呈现和响应UI。

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

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