class GoogleStaticMapsHelper::Path
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.
Constants
- OPTIONAL_OPTIONS
Attributes
Public Class Methods
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
# File lib/google_static_maps_helper/path.rb, line 29 def initialize(*args) @points = [] @encode_points = true extract_options!(args) add_points(args) end
Public Instance Methods
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.
# File lib/google_static_maps_helper/path.rb, line 93 def <<(point) @points << ensure_point_is_location_object(point) @points.uniq! self end
# File lib/google_static_maps_helper/path.rb, line 82 def clear @points = [] end
# File lib/google_static_maps_helper/path.rb, line 70 def each @points.each {|p| yield p} end
# File lib/google_static_maps_helper/path.rb, line 78 def empty? length == 0 end
Will answer the question if we are encoding the points or not when building the image URL.
# File lib/google_static_maps_helper/path.rb, line 103 def encoding_points? return !!@encode_points end
# File lib/google_static_maps_helper/path.rb, line 74 def length @points.length end
Sets the points of this Path
.
WARNING Using this method will clear out any points which might be set.
# File lib/google_static_maps_helper/path.rb, line 64 def points=(array) raise ArgumentError unless array.is_a? Array @points = [] array.each {|point| self << point} end
Private Instance Methods
# File lib/google_static_maps_helper/path.rb, line 130 def add_points(points) points.each {|point| self << point} end
Do we have enough points to build a path?
# File lib/google_static_maps_helper/path.rb, line 121 def can_build? length > 1 end
# File lib/google_static_maps_helper/path.rb, line 134 def encoded_url_points encoder = GMapPolylineEncoder.new points_as_array = points.map { |location| [location.lat, location.lng]} result = encoder.encode(points_as_array) "enc:#{result[:points]}" end
Extracts the lng and lat values from incomming point and creates a new Location
object from it.
# File lib/google_static_maps_helper/path.rb, line 113 def ensure_point_is_location_object(point) return point if point.instance_of? Location Location.new(point) end
# File lib/google_static_maps_helper/path.rb, line 125 def extract_options!(args) options = args.last.is_a?(Hash) ? args.pop : {} options.each_pair {|k, v| send("#{k}=", v)} end
# File lib/google_static_maps_helper/path.rb, line 142 def unencoded_url_points inject([]) do |point_params, point| point_params << point.to_url end.join('|') end