Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow to_g_polygon to combine into a single object in Google Maps API 3.

  • Loading branch information...
commit c4f3dc52e79348e2f84b9efb1500432c11dba401 1 parent 1b33828
@dark-panda dark-panda authored
Showing with 108 additions and 24 deletions.
  1. +54 −24 lib/geos/google_maps/api_3.rb
  2. +54 −0 test/google_maps_api_3_test.rb
View
78 lib/geos/google_maps/api_3.rb
@@ -1,6 +1,9 @@
-module Geos::GoogleMaps::Api3
- module Geometry
+module Geos::GoogleMaps
+ module Api3
+ end
+
+ module Api3Constants
UNESCAPED_MARKER_OPTIONS = %w{
icon
map
@@ -9,6 +12,20 @@ module Geometry
shape
}.freeze
+ UNESCAPED_POLY_OPTIONS = %w{
+ clickable
+ fillOpacity
+ geodesic
+ map
+ path
+ paths
+ strokeOpacity
+ strokeWeight
+ zIndex
+ }.freeze
+ end
+
+ module Api3::Geometry
# Returns a new LatLngBounds object with the proper LatLngs in place
# for determining the geometry bounds.
def to_g_lat_lng_bounds_api3(options = {})
@@ -42,25 +59,13 @@ def to_g_marker_api3(marker_options = {}, options = {})
opts = Geos::Helper.camelize_keys(marker_options)
opts[:position] = self.centroid.to_g_lat_lng(options[:lat_lng_options])
- json = Geos::Helper.escape_json(opts, UNESCAPED_MARKER_OPTIONS - options[:escape])
+ json = Geos::Helper.escape_json(opts, Geos::GoogleMaps::Api3Constants::UNESCAPED_MARKER_OPTIONS - options[:escape])
"new google.maps.Marker(#{json})"
end
end
- module CoordinateSequence
- UNESCAPED_POLY_OPTIONS = %w{
- clickable
- fillOpacity
- geodesic
- map
- path
- paths
- strokeOpacity
- strokeWeight
- zIndex
- }.freeze
-
+ module Api3::CoordinateSequence
# Returns a Ruby Array of LatLngs.
def to_g_lat_lng_api3(options = {})
self.to_a.collect do |p|
@@ -89,7 +94,7 @@ def to_g_polyline_api3(polyline_options = {}, options = {})
opts = Geos::Helper.camelize_keys(polyline_options)
opts[:path] = "[#{self.to_g_lat_lng_api3(options[:lat_lng_options]).join(', ')}]"
- json = Geos::Helper.escape_json(opts, UNESCAPED_POLY_OPTIONS - options[:escape])
+ json = Geos::Helper.escape_json(opts, Geos::GoogleMaps::Api3Constants::UNESCAPED_POLY_OPTIONS - options[:escape])
"new google.maps.Polyline(#{json})"
end
@@ -115,13 +120,13 @@ def to_g_polygon_api3(polygon_options = {}, options = {})
opts = Geos::Helper.camelize_keys(polygon_options)
opts[:paths] = "[#{self.to_g_lat_lng_api3(options[:lat_lng_options]).join(', ')}]"
- json = Geos::Helper.escape_json(opts, UNESCAPED_POLY_OPTIONS - options[:escape])
+ json = Geos::Helper.escape_json(opts, Geos::GoogleMaps::Api3Constants::UNESCAPED_POLY_OPTIONS - options[:escape])
"new google.maps.Polygon(#{json})"
end
end
- module Point
+ module Api3::Point
# Returns a new LatLng.
def to_g_lat_lng_api3(options = {})
no_wrap = if options[:no_wrap]
@@ -137,7 +142,7 @@ def to_g_point_api3(options = {})
end
end
- module Polygon
+ module Api3::Polygon
# Returns a Polyline of the exterior ring of the Polygon. This does
# not take into consideration any interior rings the Polygon may
# have.
@@ -153,7 +158,7 @@ def to_g_polygon_api3(polygon_options = {}, options = {})
end
end
- module GeometryCollection
+ module Api3::GeometryCollection
# Returns a Ruby Array of Polylines for each geometry in the
# collection.
def to_g_polyline_api3(polyline_options = {}, options = {})
@@ -161,14 +166,39 @@ def to_g_polyline_api3(polyline_options = {}, options = {})
p.to_g_polyline_api3(polyline_options, options)
end
end
+ alias :to_g_polylines_api3 :to_g_polyline_api3
# Returns a Ruby Array of Polygons for each geometry in the
- # collection.
+ # collection. If the :single option is set, a single Polygon object will
+ # be returned with all of the geometries set in the Polygon's "path"
+ # attribute. You can also use to_g_polygon_single for the same effect.
def to_g_polygon_api3(polygon_options = {}, options = {})
- self.collect do |p|
- p.to_g_polygon_api3(polygon_options, options)
+ if options[:single]
+ self.to_g_polygon_single_api3(polygon_options, options)
+ else
+ self.collect do |p|
+ p.to_g_polygon_api3(polygon_options, options)
+ end
end
end
+
+ # Behaves the same as to_g_polygon_api3 with the :single option set, where
+ # a single Google Maps Polygon will be returned with all of the Polygons
+ # set in the Polygon's "path" attribute.
+ def to_g_polygon_single_api3(polygon_options = {}, options = {})
+ options = {
+ :escape => [],
+ :lat_lng_options => {}
+ }.merge(options)
+
+ opts = Geos::Helper.camelize_keys(polygon_options)
+ opts[:paths] = %{[#{self.collect { |p|
+ "[#{p.exterior_ring.coord_seq.to_g_lat_lng_api3(options[:lat_lng_options]).join(', ')}]"
+ }.join(', ')}]}
+ json = Geos::Helper.escape_json(opts, Geos::GoogleMaps::Api3Constants::UNESCAPED_POLY_OPTIONS - options[:escape])
+
+ "new google.maps.Polygon(#{json})"
+ end
end
end
View
54 test/google_maps_api_3_test.rb
@@ -64,6 +64,60 @@ def test_to_g_polygon
)
end
+ def test_to_g_polygon_with_multi_polygon
+ multi_polygon = Geos.read(
+ 'MULTIPOLYGON(
+ ((0 0, 0 5, 5 5, 5 0, 0 0)),
+ ((10 10, 10 15, 15 15, 15 10, 10 10)),
+ ((20 20, 20 25, 25 25, 25 20, 20 20))
+ )'
+ )
+ options = {
+ :stroke_color => '#b00b1e',
+ :stroke_weight => 5,
+ :stroke_opacity => 0.5,
+ :fill_color => '#b00b1e',
+ :map => 'map'
+ }
+
+ assert_equal(
+ ["new google.maps.Polygon({\"strokeColor\": \"#b00b1e\", \"paths\": [new google.maps.LatLng(0.0, 0.0), new google.maps.LatLng(5.0, 0.0), new google.maps.LatLng(5.0, 5.0), new google.maps.LatLng(0.0, 5.0), new google.maps.LatLng(0.0, 0.0)], \"fillColor\": \"#b00b1e\", \"strokeWeight\": 5, \"map\": map, \"strokeOpacity\": 0.5})",
+ "new google.maps.Polygon({\"strokeColor\": \"#b00b1e\", \"paths\": [new google.maps.LatLng(10.0, 10.0), new google.maps.LatLng(15.0, 10.0), new google.maps.LatLng(15.0, 15.0), new google.maps.LatLng(10.0, 15.0), new google.maps.LatLng(10.0, 10.0)], \"fillColor\": \"#b00b1e\", \"strokeWeight\": 5, \"map\": map, \"strokeOpacity\": 0.5})",
+ "new google.maps.Polygon({\"strokeColor\": \"#b00b1e\", \"paths\": [new google.maps.LatLng(20.0, 20.0), new google.maps.LatLng(25.0, 20.0), new google.maps.LatLng(25.0, 25.0), new google.maps.LatLng(20.0, 25.0), new google.maps.LatLng(20.0, 20.0)], \"fillColor\": \"#b00b1e\", \"strokeWeight\": 5, \"map\": map, \"strokeOpacity\": 0.5})"],
+ multi_polygon.to_g_polygon(
+ :stroke_color => '#b00b1e',
+ :stroke_weight => 5,
+ :stroke_opacity => 0.5,
+ :fill_color => '#b00b1e',
+ :map => 'map'
+ )
+ )
+
+ assert_equal(
+ "new google.maps.Polygon({\"strokeColor\": \"#b00b1e\", \"paths\": [[new google.maps.LatLng(0.0, 0.0), new google.maps.LatLng(5.0, 0.0), new google.maps.LatLng(5.0, 5.0), new google.maps.LatLng(0.0, 5.0), new google.maps.LatLng(0.0, 0.0)], [new google.maps.LatLng(10.0, 10.0), new google.maps.LatLng(15.0, 10.0), new google.maps.LatLng(15.0, 15.0), new google.maps.LatLng(10.0, 15.0), new google.maps.LatLng(10.0, 10.0)], [new google.maps.LatLng(20.0, 20.0), new google.maps.LatLng(25.0, 20.0), new google.maps.LatLng(25.0, 25.0), new google.maps.LatLng(20.0, 25.0), new google.maps.LatLng(20.0, 20.0)]], \"fillColor\": \"#b00b1e\", \"strokeWeight\": 5, \"map\": map, \"strokeOpacity\": 0.5})",
+ multi_polygon.to_g_polygon({
+ :stroke_color => '#b00b1e',
+ :stroke_weight => 5,
+ :stroke_opacity => 0.5,
+ :fill_color => '#b00b1e',
+ :map => 'map'
+ }, {
+ :single => true
+ })
+ )
+
+ assert_equal(
+ "new google.maps.Polygon({\"strokeColor\": \"#b00b1e\", \"paths\": [[new google.maps.LatLng(0.0, 0.0), new google.maps.LatLng(5.0, 0.0), new google.maps.LatLng(5.0, 5.0), new google.maps.LatLng(0.0, 5.0), new google.maps.LatLng(0.0, 0.0)], [new google.maps.LatLng(10.0, 10.0), new google.maps.LatLng(15.0, 10.0), new google.maps.LatLng(15.0, 15.0), new google.maps.LatLng(10.0, 15.0), new google.maps.LatLng(10.0, 10.0)], [new google.maps.LatLng(20.0, 20.0), new google.maps.LatLng(25.0, 20.0), new google.maps.LatLng(25.0, 25.0), new google.maps.LatLng(20.0, 25.0), new google.maps.LatLng(20.0, 20.0)]], \"fillColor\": \"#b00b1e\", \"strokeWeight\": 5, \"map\": map, \"strokeOpacity\": 0.5})",
+ multi_polygon.to_g_polygon_single(
+ :stroke_color => '#b00b1e',
+ :stroke_weight => 5,
+ :stroke_opacity => 0.5,
+ :fill_color => '#b00b1e',
+ :map => 'map'
+ )
+ )
+ end
+
def test_to_g_polyline
assert_equal(
"new google.maps.Polyline({\"path\": [new google.maps.LatLng(0.0, 0.0), new google.maps.LatLng(1.0, 1.0), new google.maps.LatLng(2.5, 2.5), new google.maps.LatLng(5.0, 5.0), new google.maps.LatLng(0.0, 0.0)]})",
Please sign in to comment.
Something went wrong with that request. Please try again.