Class: GoogleStaticMapsHelper::Path

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/google_static_maps_helper/path.rb

Overview

A Path is used to draw things in the map, either lines or Polygons. It is build up of points and if a fill color is set you’ll get a Polygon.

Constant Summary collapse

OPTIONAL_OPTIONS =
[:weight, :color, :fillcolor]

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Path

Creates a new Path which you can push points on to to make up lines or polygons

The following options are available, for more information see the Google API documentation.

:weight

The weight is the thickness of the line, defaults to 5

:color

The color of the border can either be a textual representation like red, green, blue, black etc or as a 24-bit (0xAABBCC) or 32-bit hex value (0xAABBCCDD). When 32-bit values are given the two last bits will represent the alpha transparency value.

:fillcolor

With the fill color set you’ll get a polygon in the map. The color value can be the same as described in the :color. When used, the static map will automatically create a closed shape.

:points

An array of points. You can mix objects responding to lng and lat, and a Hash with lng and lat keys.



28
29
30
31
32
33
# File 'lib/google_static_maps_helper/path.rb', line 28

def initialize(*args)
  @points = []

  extract_options!(args)
  add_points(args)
end

Instance Method Details

#<<(point) ⇒ Object

Pushes a new point into the Path

A point might be a Hash which has lng and lat as keys, or an object responding to lng and lat. Any points pushed in will be converted internally to a Location object.



87
88
89
90
91
# File 'lib/google_static_maps_helper/path.rb', line 87

def <<(point)
  @points << ensure_point_is_location_object(point)
  @points.uniq!
  self
end

#eachObject



68
69
70
# File 'lib/google_static_maps_helper/path.rb', line 68

def each
  @points.each {|p| yield p}
end

#empty?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/google_static_maps_helper/path.rb', line 76

def empty?
  length == 0
end

#lengthObject



72
73
74
# File 'lib/google_static_maps_helper/path.rb', line 72

def length
  @points.length
end

#points=(array) ⇒ Object

Sets the points of this Path.

WARNING Using this method will clear out any points which might be set.

Raises:

  • (ArgumentError)


62
63
64
65
66
# File 'lib/google_static_maps_helper/path.rb', line 62

def points=(array)
  raise ArgumentError unless array.is_a? Array
  @points = []
  array.each {|point| self << point}
end

#url_paramsObject

Returns a string representation of this Path Used by the Map when building the URL

Raises:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/google_static_maps_helper/path.rb', line 39

def url_params # :nodoc:
  raise BuildDataMissing, "Need at least 2 points to create a path!" unless can_build?
  out = 'path='
 
  path_params = OPTIONAL_OPTIONS.inject([]) do |path_params, attribute|
    value = send(attribute)
    path_params << "#{attribute}:#{URI.escape(value.to_s)}" unless value.nil?
    path_params
  end.join('|')

  out += "#{path_params}|" unless path_params.empty?

  out += inject([]) do |point_params, point|
    point_params << point.to_url
  end.join('|')
end