Class: Kluster::Clusterer

Inherits:
Object
  • Object
show all
Includes:
Geocoder::Calculations
Defined in:
lib/kluster/clusterer.rb

Instance Method Summary collapse

Constructor Details

#initialize(pins) ⇒ Clusterer

Returns a new instance of Clusterer.



8
9
10
# File 'lib/kluster/clusterer.rb', line 8

def initialize(pins)
  @pins = pins
end

Instance Method Details

#clusterize(radius) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/kluster/clusterer.rb', line 12

def clusterize(radius)
  clustered_annotations = []
  @pins.each do |pin|
    is_containing = false
    if clustered_annotations.empty?
      new_cluster = Cluster.new(pin)
      clustered_annotations << new_cluster
    else
      clustered_annotations.each do |cluster|
        if location_near_to_other_location(pin.coordinate, cluster.coordinate, radius)
          is_containing = true
          cluster.pins << pin
          break
        end
      end

      if !is_containing
        new_cluster = Cluster.new(pin)
        clustered_annotations << new_cluster
      end
    end
  end

  return_array = []
  clustered_annotations.each do |cluster|
    if cluster.pins.length <= 1
       return_array << cluster.pins.last
    else
      return_array << cluster
    end
  end
  return_array
end