class LeafletHelper::ManageMarkers

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.

Public Class Methods

new() click to toggle source

Manage an Array of Hashes

# File lib/leaflet_helper/manage_markers.rb, line 13
def initialize()
  @markers = Array.new
end

Public Instance Methods

<<(id:, lat:, lon:, html: '<p>Anything with {variable} markers to be replaced by data contents.</p>', data:{})
Alias for: add
add(id:, lat:, lon:, html: '<p>Anything with {variable} markers to be replaced by data contents.</p>', data:{}) click to toggle source

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

# 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
Also aliased as: insert, <<, push
clean()
Alias for: clear
clear() click to toggle source

Clear out the array so we can start over

# File lib/leaflet_helper/manage_markers.rb, line 63
def clear
  @markers = Array.new
end
Also aliased as: clean
delete(id)
Alias for: remove
insert(id:, lat:, lon:, html: '<p>Anything with {variable} markers to be replaced by data contents.</p>', data:{})
Alias for: add
pull(id)
Alias for: remove
push(id:, lat:, lon:, html: '<p>Anything with {variable} markers to be replaced by data contents.</p>', data:{})
Alias for: add
remove(id) click to toggle source

Remove all marker entries that have the given id

# 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
Also aliased as: delete, rm, pull
replace_all_with(a_hash) click to toggle source
# 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) click to toggle source

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

# 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
rm(id)
Alias for: remove
to_json() click to toggle source

Turn the array of markers into a JSON structure

# File lib/leaflet_helper/manage_markers.rb, line 70
def to_json
  @markers.to_json
end