首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >他死了,吉姆:谷歌地图上可以绘制的圆圈的数量有内存限制吗?

他死了,吉姆:谷歌地图上可以绘制的圆圈的数量有内存限制吗?
EN

Stack Overflow用户
提问于 2015-06-14 19:50:04
回答 1查看 111关注 0票数 0

在一张美国地图上,我被要求画50,000个圆,每个圆的半径为5000码。

纬度位置分散在全国各地,但这些圆圈中有大量重叠。不透明度设置为0.05;具有许多叠加圆的区域变得更不透明。

圆圈开始出现,但大约30秒后,Chrome崩溃,显示“他死了,吉姆”的消息。

EN

回答 1

Stack Overflow用户

发布于 2015-06-16 18:44:24

关于错误消息:

根据Error: "He's Dead, Jim!"的说法

你可能会看到“他死了,

!”在以下情况下,选项卡中的消息:

  • 内存不足,无法运行该选项卡。计算机依靠内存来运行应用程序、扩展程序和程序。内存不足会导致它们运行缓慢或停止工作。
  • 您可以使用谷歌Chrome的任务管理器、系统的任务管理器或命令行工具停止进程。

显然,这是因为您正在尝试呈现50k对象。为了渲染如此多的对象,我建议考虑使用Overlay approach。在这种情况下,由于城市图标可以通过canvas元素而不是div元素呈现,因此性能可以得到显着提高。

话虽如此,下面的示例演示了如何使用所描述的方法渲染多个城市(1000城市):

代码语言:javascript
运行
复制
var overlay;
USCitiesOverlay.prototype = new google.maps.OverlayView();

function USCitiesOverlay(map) {
    this._map = map;
    this._cities = [];
    this._radius = 6;
    this._container = document.createElement("div");
    this._container.id = "citieslayer";
    this.setMap(map);
    this.addCity = function (lat, lng) {
        this._cities.push({position: new google.maps.LatLng(lat,lng)});
    };
}


USCitiesOverlay.prototype.createCityIcon = function (id,pos) {
    /*var cityIcon = document.createElement('div');
    cityIcon.setAttribute('id', "cityicon_" + id);
    cityIcon.style.position = 'absolute';
    cityIcon.style.left = (pos.x - this._radius) + 'px';
    cityIcon.style.top = (pos.y - this._radius) + 'px';
    cityIcon.style.width = cityIcon.style.height = (this._radius * 2) + 'px';
    cityIcon.className = "circleBase city";
    return cityIcon;*/

   
    var cityIcon = document.createElement('canvas');

    cityIcon.id = 'cityicon_' + id;
    cityIcon.width = cityIcon.height = this._radius * 2;
    cityIcon.style.width = cityIcon.width + 'px';
    cityIcon.style.height = cityIcon.height + 'px';
    cityIcon.style.left = (pos.x - this._radius) + 'px';  
    cityIcon.style.top = (pos.y - this._radius) + 'px'; 
    cityIcon.style.position = "absolute";

    var centerX = cityIcon.width / 2;
    var centerY = cityIcon.height / 2;
    var ctx = cityIcon.getContext('2d');
    ctx.fillStyle = 'rgba(160,16,0,0.6)';
    ctx.beginPath();
    ctx.arc(centerX, centerY, this._radius, 0, Math.PI * 2, true);
    ctx.fill();

    return cityIcon;
};    


USCitiesOverlay.prototype.ensureCityIcon = function (id,pos) {
    var cityIcon = document.getElementById("cityicon_" + id);
    if(cityIcon){
        cityIcon.style.left = (pos.x - this._radius) + 'px';
        cityIcon.style.top = (pos.y - this._radius) + 'px';
        return cityIcon;
    }
    return this.createCityIcon(id,pos);
};    



USCitiesOverlay.prototype.onAdd = function () {
    var panes = this.getPanes();
    panes.overlayLayer.appendChild(this._container);
};



USCitiesOverlay.prototype.draw = function () {
    var zoom = this._map.getZoom();
    var overlayProjection = this.getProjection();

    var container = this._container;
    
    this._cities.forEach(function(city,idx){
        var xy = overlayProjection.fromLatLngToDivPixel(city.position);
        var cityIcon = overlay.ensureCityIcon(idx,xy);
        container.appendChild(cityIcon);    
    });
   
};

USCitiesOverlay.prototype.onRemove = function () {
    this._container.parentNode.removeChild(this._container);
    this._container = null;
};







function getRandomInterval(min, max) {
    return Math.random() * (max - min) + min;
}


function generateCityMap(count) {
    var citymap = [];

    var minPos = new google.maps.LatLng(49.25, -123.1);
    var maxPos = new google.maps.LatLng(34.052234, -74.005973);
    

    for(var i = 0; i < count;i++)
    {
       var lat = getRandomInterval(minPos.lat(),maxPos.lat());
       var lng = getRandomInterval(minPos.lng(),maxPos.lng());
       var population = getRandomInterval(10000,1000000);


       citymap.push({
          location: new google.maps.LatLng(lat, lng),
          population: population
       });

    }

    return citymap;
}




function initialize() {
    var mapOptions = {
        zoom: 4,
        center: new google.maps.LatLng(37.09024, -95.712891),
        mapTypeId: google.maps.MapTypeId.TERRAIN
    };

    var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

    overlay = new USCitiesOverlay(map);
    overlay.addCity(40.714352, -74.005973);   //chicago
    overlay.addCity(40.714352, -74.005973);   //newyork
    overlay.addCity(34.052234, -118.243684);   //losangeles
    overlay.addCity(49.25, -123.1);   //vancouver

    var citymap = generateCityMap(1000);
    citymap.forEach(function(city){
          overlay.addCity(city.location.lat(), city.location.lng());   
    });    

}


google.maps.event.addDomListener(window, 'load', initialize);
代码语言:javascript
运行
复制
html, body, #map-canvas {
    height: 100%;
    margin: 0px;
    padding: 0px;
}
代码语言:javascript
运行
复制
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>
<div id="map-canvas"></div>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30828994

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档