Class: LeafletHelper::ManageMarkers

Inherits:
Object
  • Object
show all
Defined in:
lib/leaflet_helper/manage_markers.rb

Overview

Marker encapsulates an Array of Hashes where each Hash is a marker structure which can be used to add content to a LeafletJS-managed map image.

Instance Method Summary collapse

Constructor Details

#initializeManageMarkers

Manage an Array of Hashes



13
14
15
# File 'lib/leaflet_helper/manage_markers.rb', line 13

def initialize()
  @markers = Array.new
end

Instance Method Details

#add(id:, lat:, lon:, html: '<p>Anything with {variable} markers to be replaced by data contents.</p>', data: {}) ⇒ Object Also known as: insert, <<, push

a marker consists of an identification (not unique), a location expressed as decimal latitude longitude and an html component.



21
22
23
24
25
26
27
28
# File 'lib/leaflet_helper/manage_markers.rb', line 21

def add(id:, lat:, lon:, html: '<p>Anything with {variable} markers to be replaced by data contents.</p>', data:{})
  @markers << {
    id:   id,
    lat:  lat,
    lon:  lon,
    html: data.empty? ? html : StringTemplate.new(html).use(data)
  }
end

#clearObject Also known as: clean

Clear out the array so we can start over



63
64
65
# File 'lib/leaflet_helper/manage_markers.rb', line 63

def clear
  @markers = Array.new
end

#remove(id) ⇒ Object Also known as: delete, rm, pull

Remove all marker entries that have the given id



35
36
37
38
39
40
41
# File 'lib/leaflet_helper/manage_markers.rb', line 35

def remove(id)
  return() if @markers.empty?
  @markers.each_index do |x|
    @markers[x] = nil if id == @markers[x][:id]
  end
  @markers.compact!
end

#replace_all_with(a_hash) ⇒ Object



57
58
59
# File 'lib/leaflet_helper/manage_markers.rb', line 57

def replace_all_with(a_hash)
  replace_with(:all, a_hash)
end

#replace_with(id, a_hash) ⇒ Object

Replace the html string’s variables with values from a hash



48
49
50
51
52
53
54
55
# File 'lib/leaflet_helper/manage_markers.rb', line 48

def replace_with(id, a_hash)
  return() if @markers.empty?
  @markers.each_index do |x|
    if :all == id || id == @markers[x][:id]
      @markers[x][:html] = StringTemplate.new(@markers[x][:html]).use(a_hash)
    end
  end
end

#to_jsonObject

Turn the array of markers into a JSON structure



70
71
72
# File 'lib/leaflet_helper/manage_markers.rb', line 70

def to_json
  @markers.to_json
end