Skip to content

Instantly share code, notes, and snippets.

@guyboertje
Created April 13, 2012 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guyboertje/2378200 to your computer and use it in GitHub Desktop.
Save guyboertje/2378200 to your computer and use it in GitHub Desktop.
var WeightedMarkerClusters;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
(function($, w) {}, WeightedMarkerClusters = (function() {
function WeightedMarkerClusters(jq_element, options) {
this.ele = jq_element;
this.opts = options;
this.markerCluster = null;
this.entity_id = this.ele.parent().find(".context").eq(0).data("entityId");
this.map = this.ele.find(this.opts.map_selector).eq(0);
this.map.gmap3({
action: "init",
options: {
center: this.opts.map_center,
zoom: this.opts.map_zoom_level,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
});
this.gmap = this.map.gmap3({
action: "get"
});
this.markerCluster = new MarkerClusterer(this.gmap);
this.markerCluster.setGridSize(this.opts.cluster_grid_size);
this.markerCluster.setMinimumClusterSize(this.opts.min_cluster_size);
this.markerCluster.setCalculator(this.calculator);
$.ajax({
url: w.Router.weighted_marker_clusters(this.entity_id),
accepts: "application/vnd.music-glue-user-app-v1+json",
cache: false
}).done(__bind(function(data) {
var point, _i, _len, _results;
_results = [];
for (_i = 0, _len = data.length; _i < _len; _i++) {
point = data[_i];
_results.push(this.add_marker(point));
}
return _results;
}, this));
}
WeightedMarkerClusters.prototype.add_marker = function(point) {
var latLng, marker;
latLng = new google.maps.LatLng(point.lat, point.lng);
marker = new google.maps.Marker({
position: latLng,
title: "" + point.weight,
weight: point.weight
});
return this.markerCluster.addMarker(marker);
};
WeightedMarkerClusters.prototype.calculator = function(markers, numStyles) {
var count, dv, index, mark, _i, _len;
index = 0;
count = 0;
for (_i = 0, _len = markers.length; _i < _len; _i++) {
mark = markers[_i];
count += mark.weight;
}
dv = count.toString();
while (dv !== 0) {
dv = parseInt(dv / 10, 10);
index++;
}
index = Math.min(index, numStyles);
return {
text: count,
index: index
};
};
return WeightedMarkerClusters;
})(), $.fn.extend({
weighted_marker_clusters: function(options) {
var opts;
if (options == null) {
options = {};
}
opts = $.extend({}, $.fn.weighted_marker_clusters.defaults, options);
return this.each(function() {
var $this;
$this = $(this);
if ($this.length && !$.data($this, "plugin_weighted_marker_clusters")) {
return $.data($this, "plugin_weighted_marker_clusters", new WeightedMarkerClusters($this, opts));
}
});
}
}), $.fn.weighted_marker_clusters.defaults = {
map_selector: ".map_area",
cluster_grid_size: 120,
min_cluster_size: 1,
map_center: [0, 0],
map_zoom_level: 1
})(jQuery, window);
(($, w) ->
class WeightedMarkerClusters
constructor: (jq_element, options) ->
@ele = jq_element
@opts = options
@markerCluster = null
@entity_id = @ele.parent().find(".context").eq(0).data("entityId")
@map = @ele.find(@opts.map_selector).eq(0)
@map.gmap3
action: "init"
options:
center: @opts.map_center
zoom: @opts.map_zoom_level
mapTypeId: google.maps.MapTypeId.ROADMAP
@gmap = @map.gmap3
action: "get"
@markerCluster = new MarkerClusterer(@gmap)
@markerCluster.setGridSize @opts.cluster_grid_size
@markerCluster.setMinimumClusterSize @opts.min_cluster_size
@markerCluster.setCalculator @calculator
$.ajax
url: w.Router.weighted_marker_clusters(@entity_id)
accepts: "application/vnd.music-glue-user-app-v1+json"
cache: false
.done (data) =>
@add_marker(point) for point in data
add_marker: (point) ->
latLng = new google.maps.LatLng(point.lat, point.lng)
marker = new google.maps.Marker(position: latLng, title: ""+point.weight, weight: point.weight)
@markerCluster.addMarker marker
calculator: (markers, numStyles) ->
index = 0
count = 0
count += mark.weight for mark in markers
dv = count.toString()
while dv isnt 0
dv = parseInt(dv / 10, 10)
index++
index = Math.min(index, numStyles)
text: count
index: index
$.fn.extend
weighted_marker_clusters: (options = {}) ->
opts = $.extend {}, $.fn.weighted_marker_clusters.defaults, options
@each ->
$this = $(@)
if $this.length and not $.data($this, "plugin_weighted_marker_clusters")
$.data $this, "plugin_weighted_marker_clusters", new WeightedMarkerClusters($this, opts)
$.fn.weighted_marker_clusters.defaults =
map_selector: ".map_area"
cluster_grid_size: 120
min_cluster_size: 1
map_center: [0, 0]
map_zoom_level: 1
)(jQuery, window)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment