class GoogleStaticMapClusters::Map

Public Class Methods

new(opts = {}) click to toggle source
# File lib/googlestaticmap-clusters.rb, line 10
def initialize(opts = {})
  defaults = {
      :sensor => false,
      :map_width =>600,
      :map_height => 600,
      :style => {:marker_size => 'mid', :marker_color => 'blue'},
      :zoom => 12,
      :max_distance => 50, # distance for clustering
      :outliers_distance => 700 # distance for cleaning outliers
  }

  @layers = []
  @opts = defaults.merge(opts)
end

Public Instance Methods

add_layer(points,opts={}) click to toggle source
# File lib/googlestaticmap-clusters.rb, line 25
def add_layer(points,opts={})
  options = @opts.merge(opts)
  if options[:outliers_distance]
    points = clean_outliers(points,options[:outliers_distance],options[:zoom])
  end
  c = RClusters::ScreenDistance.new({:max_distance=>options[:max_distance],
                                     :zoom=>options[:zoom]})
  @layers.push({:clusters => (c.calculate(points)),:style => options[:style]})
end

Private Instance Methods

centroid(points) click to toggle source
# File lib/googlestaticmap-clusters.rb, line 70
def centroid(points)
  return if points.empty?
  lats = points.inject(0) {|sum, n| sum + n[:lat]}
  lons = points.inject(0) {|sum, n| sum + n[:lon]}
  {:lat => lats/points.count, :lon => lons/points.count}
end
clean_outliers(points, max_distance, zoom) click to toggle source
# File lib/googlestaticmap-clusters.rb, line 57
def clean_outliers(points, max_distance, zoom)
  centroid = centroid(points)
  points.map{|point|
    pixel_distance = PixelDistance::from_coords(point[:lat], point[:lon], centroid[:lat], centroid[:lon], zoom)
    if pixel_distance > max_distance
      nil
    else
      point
    end

  }.compact
end