MeteorJS和UnderscoreJS:将结果分组以在Google Map上绘制点 [英] MeteorJS and UnderscoreJS: grouping results to plot points on a Google Map
问题描述
我正在开发一个小型MeteorJS应用程序,该应用程序根据受欢迎的工作区域在地图上绘制点.
I am working on a small MeteorJS app that plots points on a map based on popular areas for work.
我有这个:
Template.list.jobs = function() {
if(Session.get('currentIndustryOnet')) {
jobs = Jobs.find({onet: Session.get('currentIndustryOnet')}).fetch();
// Session.set('jobCount', jobs.count());
var cnt = _.groupBy(jobs, 'address');
console.log(cnt);
return Pagination.collection(jobs);
} else {
jobs = Jobs.find()
Session.set('jobCount', jobs.count());
return Pagination.collection(jobs.fetch());
}
}
cnt
变量返回一个正确分组的数组(该数组的键是一个地址,例如 Allentown,PA
).我收集了一些美国曾经有过的城市及其LAT/LONG,可以在Google地图上进行绘制.因此,我将从分组数组中获得前100名,在Cities集合中找到经度/纬度,然后在地图上绘制这些点.
The cnt
variable returns a properly grouped array (the key of the array is an address like Allentown, PA
). I have a collection of Cities which have ever city in the USA along with their LAT/LONGs to plot on a Google Map. So I will take the top 100 from the grouped array, find the lat/long in the Cities collection and plot those points on a map.
我不熟悉使用 groupedBy
方法根据长度对列表进行排序,然后拉出用作我的搜索键的方法.
I am not familiar with working with a groupedBy
method to sort the list based on the length and then pull out the key to use as my search.
推荐答案
我不确定100%的数据结构...但是假设作业具有 address
字段,而您想要它们按出现频率排序并设置上限,您可以执行以下操作:
I'm not 100% certain about the data structure... but assuming jobs have an address
field, and you want them sorted by frequency of occurrence and capped, you could do something like this:
var addresses = _.chain(jobs)
.countBy('address')
.pairs()
.sortBy(function(j) {return -j[1];})
.map(function(j) {return j[0];})
.first(100)
.value();
请注意,使用下划线可能会有更巧妙的方法来得出此结果.一旦获得了有上限的地址列表,您就可以通过类似以下的查找来获取经度/纬度值:
Note there may be a more clever way to use underscore to arrive at this result. Once you have the capped, sorted list of addresses, you can probably get the lat/long values via a find like:
Cities.find({address: {$in: addresses}}).fetch();
这篇关于MeteorJS和UnderscoreJS:将结果分组以在Google Map上绘制点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!