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.

<tt>:encode_points

A flag which tells us if we should encode the points in this path or not. Defaults to true



30
31
32
33
34
35
36
# File 'lib/google_static_maps_helper/path.rb', line 30

def initialize(*args)
  @points = []
  @encode_points = true

  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.



94
95
96
97
98
# File 'lib/google_static_maps_helper/path.rb', line 94

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

#clearObject



83
84
85
# File 'lib/google_static_maps_helper/path.rb', line 83

def clear
  @points = []
end

#eachObject



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

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

#empty?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/google_static_maps_helper/path.rb', line 79

def empty?
  length == 0
end

#encoding_points?Boolean

Will answer the question if we are encoding the points or not when building the image URL.

Returns:

  • (Boolean)


104
105
106
# File 'lib/google_static_maps_helper/path.rb', line 104

def encoding_points?
  return !!@encode_points
end

#lengthObject



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

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)


65
66
67
68
69
# File 'lib/google_static_maps_helper/path.rb', line 65

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:



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

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 += encoded_url_points if encoding_points?
  out += unencoded_url_points unless encoding_points?
  out
end