Permalink
Browse files

Added support for encoded values using :encoded option on Google::Pol…

…ygon
  • Loading branch information...
1 parent 505aff4 commit f4a1badd193f045aa5fe8b1e39f7812558c0a0e8 @yawningman yawningman committed Dec 9, 2008
@@ -341,7 +341,9 @@ def add_polygon(options)
self.add_overlay polygon
polygon.added_to_map self
- self.extend_track_bounds polygon.vertices
+ unless polygon.encoded?
+ self.extend_track_bounds polygon.vertices
+ end
polygon
end
@@ -3,15 +3,46 @@ module Google
# Represents a polygon that can be added to a Map using Map#add_polygon. If a method or event is not documented here please
# see googles online[http://code.google.com/apis/maps/documentation/reference.html#GPolygon] docs for details.
# See MapObject#listen_to on how to use events not listed on this object.
+ #
+ #
+ # === Encoding examples:
+ #
+ # # Draw around the Pentagon using the default styling.
+ # polygon = Google::Polygon.new(:encoded => {:points => 'ihglFxjiuMkAeSzMkHbJxMqFfQaOoB', :levels => 'PFHFGP',
+ # :num_levels => 18, :zoom_factor => 2})
+ #
+ # # Draw the shape of the Pentagon using two encoded polylines with styling
+ # polygon = Google::Polygon.new(:encoded => [{:points => 'ihglFxjiuMkAeSzMkHbJxMqFfQaOoB', :levels => 'PFHFGP',
+ # :num_levels => 18, :zoom_factor => 2},
+ # {:points => 'cbglFhciuMY{FtDqBfCvD{AbFgEm@', :levels => 'PDFDEP',
+ # :num_levels => 18, :zoom_factor => 2}],
+ # :border_opacity => 0.5,
+ # :border_thickness => 7,
+ # :border_colour => 'orange',
+ # :fill_colour => 'green',
+ # :fill_opacity => 0.7)
class Polygon < MapObject
- attr_reader :vertices
+ attr_reader :vertices, :encoded
include Tooltipable
+ # A polygon is built either using the +vertices+ or +encoded+ options which use an array of locations or encoded points
+ # respectively.
+ #
# ==== Options:
- # * +vertices+ - Required. A single location or array of locations representing the vertices of the polygon.
+ # * +vertices+ - Optional. A single location or array of locations representing the vertices of the polygon.
# * +editable+ - Optional. Indicates if the polygon is editable, defaulted to +false+.
# * +tooltip+ - Optional. See Google::Tooltip#new for valid options.
+ #
+ # ==== Encoded options
+ # * +encoded+ - Optional. A single hash or array of hashes witht he below options, which represent encoded polylines.
+ # * +points+ - A string containing the encoded latitude and longitude coordinates.
+ # * +levels+ - A string containing the encoded polyline zoom level groups
+ # * +num_levels+ - The number of zoom levels contained in the encoded +levels+ option.
+ # * +zoom_factor+ - The magnification between adjacent sets of zoom levels in the encoded +levels+ option.
+ # * +fill+ - Optional. Indicates if the encoded polygon should be filled with the +fill_colour+, defaulted to +true+.
+ # * +outline+ - Optional. Indicates if the encoded polygon should be outlied using +border_colour+, +border_thickness+ and
+ # +border_opacity+, defaulted to +true+.
#
# ==== Styling options
# * +border_colour+ - Optional. The colour of the border, can be a name('red', 'blue') or a hex colour, defaulted to '#00F'
@@ -20,20 +51,32 @@ class Polygon < MapObject
# * +fill_colour+ - Optional. The colour that the circle is filled with, defaulted to '#66F'.
# * +fill_opacity+ - Optional. The opacity of the filled area of the circle, defaulted to 0.5.
def initialize(options = {})
- options.default! :var => 'polygon', :vertices => [],
+ options.default! :var => 'polygon',
+ :vertices => [],
:editable => false,
:border_colour => '#00F',
:border_thickness => 2,
:border_opacity => 0.5,
:fill_colour => '#66F',
- :fill_opacity => 0.5
+ :fill_opacity => 0.5,
+ # Encode options
+ :encoded => nil,
+ :fill => true,
+ :outline => true
super
if create_var?
- self.vertices = options.extract(:vertices).arify.collect do |vertex|
- Google::OptionsHelper.to_location(vertex)
- end
+ self.encoded = options.extract(:encoded)
+
+ if self.encoded?
+ self.encoded = self.encoded.arify
+ else
+ self.vertices = options.extract(:vertices).arify.collect do |vertex|
+ Google::OptionsHelper.to_location(vertex)
+ end
+ end
+
editable = options.extract(:editable)
border_colour = options.extract(:border_colour)
@@ -43,10 +86,25 @@ def initialize(options = {})
fill_colour = options.extract(:fill_colour)
fill_opacity = options.extract(:fill_opacity)
- tooltip_options = options.extract(:tooltip)
+ fill = options.extract(:fill)
+ outline = options.extract(:outline)
+ tooltip_options = options.extract(:tooltip)
+
remaining_options = options
- self << "#{self.var} = new GPolygon([#{self.vertices.join(', ')}], #{border_colour.to_js}, #{border_thickness.to_js}, #{border_opacity.to_js}, #{fill_colour.to_js}, #{fill_opacity.to_js}, #{remaining_options.to_google_options});"
+
+ if self.encoded?
+ encode_options = {:polylines => Google::OptionsHelper.to_encoded_polylines(:lines => self.encoded,
+ :color => border_colour,
+ :opacity => border_opacity,
+ :weight => border_thickness),
+ :color => fill_colour, :opacity => fill_opacity,
+ :fill => fill, :outline => outline}
+
+ self << "#{self.var} = new GPolygon.fromEncoded(#{encode_options.to_google_options(:dont_convert => [:polylines])});"
+ else
+ self << "#{self.var} = new GPolygon([#{self.vertices.join(', ')}], #{border_colour.to_js}, #{border_thickness.to_js}, #{border_opacity.to_js}, #{fill_colour.to_js}, #{fill_opacity.to_js}, #{remaining_options.to_google_options});"
+ end
self.enable_editing! if editable
self.set_tooltip(tooltip_options) if tooltip_options
@@ -59,7 +117,7 @@ def add_vertex(location)
location = Google::OptionsHelper.to_location(location)
self << "#{self.var}.insertVertex(#{self.last_vertex_index}, #{location})"
end
-
+
def click(&block)
self.listen_to :event => :click, :with => :location, &block
end
@@ -111,10 +169,14 @@ def added_to_map(map) # :nodoc:
def removed_from_map(map) # :nodoc:
self.remove_tooltip_from_map(map)
- end
+ end
+
+ # Indicates if the polygon was created using encoded polylines.
+ def encoded?
+ self.encoded.not_nil?
+ end
protected
- attr_writer :vertices
-
+ attr_writer :vertices, :encoded
end
end
@@ -96,6 +96,24 @@ def self.to_google_position(options)
"new GControlPosition(#{options[:anchor].to_google_anchor}, #{options[:offset].to_google_size})"
end
end
+
+ def self.to_encoded_polyline(options)
+ options.to_google_options
+ end
+
+ def self.to_encoded_polylines(options)
+ lines = options.extract(:lines)
+ style_options = options
+
+ # The first line's style will be used for all line styles
+ lines.first.merge!(style_options)
+
+ polylines = lines.collect do |encoded_polyline|
+ self.to_encoded_polyline encoded_polyline
+ end
+
+ "[#{polylines.join(', ')}]"
+ end
end
@@ -73,4 +73,26 @@ def test_to_line
assert_equal Google::Line, Google::OptionsHelper.to_line(:vertices => :first_location).class
end
+ def test_to_encoded_polyline
+ line = {:points => 'ihglFxjiuMkAeSzMkHbJxMqFfQaOoB', :levels => 'PFHFGP', :color => 'red',
+ :opacity => 0.7, :weight => 3, :num_levels => 18, :zoom_factor => 2}
+
+ assert_equal '{color: "red", levels: "PFHFGP", numLevels: 18, opacity: 0.7, points: "ihglFxjiuMkAeSzMkHbJxMqFfQaOoB", weight: 3, zoomFactor: 2}',
+ Google::OptionsHelper.to_encoded_polyline(line)
+
+ line = {:points => 'ihglFxjiuMkAeSzMkHbJxMqFfQaOoB', :levels => 'PFHFGP'}
+
+ assert_equal '{levels: "PFHFGP", points: "ihglFxjiuMkAeSzMkHbJxMqFfQaOoB"}',
+ Google::OptionsHelper.to_encoded_polyline(line)
+ end
+
+ def test_to_encoded_polylines
+ options = {:color => 'red', :opacity => 0.7, :weight => 3}
+ options[:lines] = [{:points => 'ihglFxjiuMkAeSzMkHbJxMqFfQaOoB', :levels => 'PFHFGP', :num_levels => 18, :zoom_factor => 2},
+ {:points => 'cbglFhciuMY{FtDqBfCvD{AbFgEm@', :levels => 'PDFDEP', :num_levels => 18, :zoom_factor => 2}]
+
+ assert_equal '[{color: "red", levels: "PFHFGP", numLevels: 18, opacity: 0.7, points: "ihglFxjiuMkAeSzMkHbJxMqFfQaOoB", weight: 3, zoomFactor: 2}, {levels: "PDFDEP", numLevels: 18, points: "cbglFhciuMY{FtDqBfCvD{AbFgEm@", zoomFactor: 2}]',
+ Google::OptionsHelper.to_encoded_polylines(options)
+ end
+
end
View
@@ -16,4 +16,28 @@ def test_center
end
end
+ def test_encoded
+ Eschaton.with_global_script do |script|
+
+ assert_output_fixture 'polygon = new GPolygon.fromEncoded({color: "#66F", fill: true, opacity: 0.5, outline: true, polylines: [{color: "#00F", levels: "PFHFGP", numLevels: 18, opacity: 0.5, points: "ihglFxjiuMkAeSzMkHbJxMqFfQaOoB", weight: 2, zoomFactor: 2}]});',
+ script.record_for_test {
+ Google::Polygon.new(:encoded => {:points => 'ihglFxjiuMkAeSzMkHbJxMqFfQaOoB', :levels => 'PFHFGP',
+ :num_levels => 18, :zoom_factor => 2})
+ }
+
+ assert_output_fixture 'polygon = new GPolygon.fromEncoded({color: "blue", fill: true, opacity: 0.6, outline: true, polylines: [{color: "red", levels: "PFHFGP", numLevels: 18, opacity: 0.3, points: "ihglFxjiuMkAeSzMkHbJxMqFfQaOoB", weight: 3, zoomFactor: 2}, {levels: "PDFDEP", numLevels: 18, points: "cbglFhciuMY{FtDqBfCvD{AbFgEm@", zoomFactor: 2}]});',
+ script.record_for_test {
+ Google::Polygon.new(:encoded => [{:points => 'ihglFxjiuMkAeSzMkHbJxMqFfQaOoB', :levels => 'PFHFGP',
+ :num_levels => 18, :zoom_factor => 2},
+ {:points => 'cbglFhciuMY{FtDqBfCvD{AbFgEm@', :levels => 'PDFDEP',
+ :num_levels => 18, :zoom_factor => 2}],
+ :border_colour => 'red',
+ :border_opacity => 0.3,
+ :border_thickness => 3,
+ :fill_colour => 'blue',
+ :fill_opacity => 0.6)
+ }
+ end
+ end
+
end

0 comments on commit f4a1bad

Please sign in to comment.