使用实时数据对GeoJSON文件中的许多标记进行样式化
Styling a lot of markers from GeoJSON File with Live Data
我有一个动态创建的GeoJSON文件,其中包含具有特定纬度/经度、时间戳和类型信息的数据。这些数据会不断更新,需要在谷歌地图上实时显示。以下是一个片段:
{ "type": "FeatureCollection","features": [
{"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [149.522, -30.3603]},
"properties": {
"id": "20447637",
"lat": "-30.3603",
"long": "149.522",
"timestamp": "1450247155",
"strokeType": "0",
"mag": "19.9"
}
},
{"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [149.555, -30.3647]},
"properties": {
"id": "20451255",
"lat": "-30.3647",
"long": "149.555",
"timestamp": "1450247646",
"strokeType": "3",
"mag": "0"
}
},...
我目前正在使用对php脚本的ajax调用导入这个geojson文件,该脚本从数据库中生成所需的数据,这一切都很好,而且是可扩展的。问题是,当我尝试对这些项目进行样式设置时,可能会导致浏览器崩溃(可能有数千个)。以下是我目前使用的样式代码:
function styleFeature(feature) {
var seconds = new Date().getTime() / 1000;
var timeElapsed = seconds - feature.getProperty('timestamp');
var fraction = timeElapsed/(TimeScale/255);
var color = 'rgb(' + Math.round((255 - fraction)) + ', 0, ' + Math.round(0 + fraction) + ')';
if(timeElapsed<15) { var stkWeight = 2; var stkColor = "#000"; } else { var stkWeight = 0.5; var stkColor = "#fff"; }
if(feature.getProperty('strokeType')==0) {
if(feature.getProperty('mag')>0) {
return {
icon: {
path: 'm5.219986,29.697235l23.795303,0l0,-25.117241l24.409424,0l0,25.117241l23.795288,0l0,25.765518l-23.795288,0l0,25.117264l-24.409424,0l0,-25.117264l-23.795303,0l0,-25.765518z',
strokeWeight: stkWeight,
strokeColor: stkColor,
fillColor: color,
fillOpacity: 0.5,
scale: 0.12 * scale
},
};
} else {
return {
icon: {
path: google.maps.SymbolPath.BACKWARD_OPEN_ARROW,
strokeWeight: stkWeight,
strokeColor: stkColor,
fillColor: color,
fillOpacity: 0.5,
scale: 1.5 * scale
},
};
}
} else {
return {
icon: {
path: google.maps.SymbolPath.CIRCLE,
strokeWeight: stkWeight,
strokeColor: stkColor,
fillColor: color,
fillOpacity: 0.5,
scale: 2 * scale
},
};
}
}
有3种不同类型的图标,其中两种是谷歌的默认图标,而一种是自定义+形状。geojson文件中的每个特性的样式如下:
- 图标的颜色取决于其时间戳(红色是当前的,而蓝色是旧的)
- "strokeType"answers"mag"字段确定要使用的图标(圆形、三角形、加号)
- 最后x秒内的新图标有一个较厚的黑色边框
这都是从map.data.addGeoJson(geoJSONDataFromAjaxCall);
之后的map.data.setStyle(styleFeature);
调用的
然后每隔x秒调用一次该脚本以检索更新的信息,然后还需要将该信息与其他信息一起绘制在地图上。超过预定时间(比如60分钟或3600秒)的数据需要删除,每个标记都需要随着年龄的增长而改变颜色。
我已经能够实现以上所有功能,但是,当视口中有几百个以上的图标时,浏览器会堵塞并崩溃。如果我使用标准标记而不设置样式,效果会很好。我正在寻找一种在不破坏浏览器的情况下设计这些图标的方法,我猜我这样做的方式非常耗费资源。
一旦对象数量(例如圆形标记图标)达到1k或以上,就会出现这种行为,性能将大幅下降。通常在这种情况下,建议通过画布平铺覆盖来渲染对象。
为此,我们引入一个自定义覆盖,用于显示标记图标
来源:iconsoverlay.js
示例
这是一个类似的示例,演示了如何加载GeoJSON数据并使用自定义图标显示标记:
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {
lat: -30.3647,
lng: 149.555
},
zoom: 4,
mapTypeId: google.maps.MapTypeId.TERRAIN
});
var overlay = new IconsOverlay(map);
//1.load GeoJSON data
var data = generateData(1000);
//2.display custom icons
data.features.forEach(function (f, id) {
var position = new google.maps.LatLng(f.geometry.coordinates[1], f.geometry.coordinates[0]);
if (id > 0 && id % 2 == 0) {
//add circle icon
var circleIconProperties = {
id: id,
path: google.maps.SymbolPath.CIRCLE,
strokeWeight: 2,
strokeColor: 'rgba(0, 0, 0, 1)',
fillColor: 'rgba(160,16,0,0.5)',
scale: 10
}
overlay.addIcon(position, circleIconProperties);
console.log(id);
}
else if (id > 0 && id % 3 == 0) {
//add path icon
var pathIconProperties = {
id: id,
path: 'm5.219986,29.697235l23.795303,0l0,-25.117241l24.409424,0l0,25.117241l23.795288,0l0,25.765518l-23.795288,0l0,25.117264l-24.409424,0l0,-25.117264l-23.795303,0l0,-25.765518z',
strokeWeight: 2,
strokeColor: 'rgba(0, 0, 0, 1)',
fillColor: 'rgba(160,16,0,0.5)',
scale: 0.4
}
overlay.addIcon(position,pathIconProperties);
} else {
var arrowIconProperties = {
id: id,
path: google.maps.SymbolPath.BACKWARD_OPEN_ARROW,
strokeColor: 'rgba(0, 0, 0, 1)',
fillColor: 'rgba(160,16,0,0.5)',
fillOpacity: 0.5,
scale: 4
}
overlay.addIcon(position, arrowIconProperties);
}
});
}
//Helper methods
function generateData(count) {
var data = {
"type": "FeatureCollection",
"features": []
};
for (var i = 0; i < count; i++) {
var lat = getRandomArbitrary(-30.0,-15.0);
var lng = getRandomArbitrary(120.0,150.0);
data.features.push({
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [lng, lat]
},
"properties": {
"id": i.toString(),
"lat": lat.toString(),
"long": lng.toString(),
}
});
}
return data;
}
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
google.maps.event.addDomListener(window, 'load', initMap);
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<script type="text/javascript" src="http://rawgit.com/vgrem/6dd435ebacd720f0ad10/raw/4af8287e17c3aea92b50681d0a9061f2b620f70d/iconsoverlay.js"></script>
<div id="map"></div>
演示页面
- 当包含另一个asp文件时,是否也包含所有引用的样式和脚本页面
- 将样式表插入iframe
- 跟踪在页面加载时应用内联样式的JavaScript
- 如果文本字段为空,则使用JavaScript应用CSS样式
- YUI3 IO实用程序是否可以根据给定的内容类型标头值自动序列化数据
- 定义完全独立的样式信息
- 如何编写一个具有公共标头的批处理
- 如何告诉MathJax对下标使用替代语法
- 正在传递JSONP标头's数据参数到另一个文件中的AJAX请求
- W3C循环样式的JavaScript
- jQuery/JavaScript在线公文包表单-打印样式表
- 使用递归、Ramda.js和无点样式重构字符串的getPermutations()
- FF和Chromium中CSS样式按钮的外观差异
- 否'访问控制允许来源'标头存在于IISNOde中请求的资源(AngularJS+NodeJs)上
- 如何首先设置样式<td>表中包含在窗体中的元素
- javascript问题正文样式
- 使用具有内联样式的tidy-html5
- 使用实时数据对GeoJSON文件中的许多标记进行样式化
- 应用许多类到内联样式的html
- 为javascript激活的标头设计样式