Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

GroundOverlay merge and refactor

  • Loading branch information...
commit 477d73ebb45ed2e512c687419ada418be56d8176 1 parent 8ee9390
@yawningman yawningman authored
View
71 README.rdoc
@@ -1,34 +1,67 @@
- .__ __
- ____ ______ ____ | |__ _____ _/ |_ ____ ____
- _/ __ \ / ___// ___\| | \\__ \\ __\/ _ \ / \
- \ ___/ \___ \\ \___| Y \/ __ \| | ( <_> ) | \
- \___ >____ >\___ >___| (____ /__| \____/|___| /
- \/ \/ \/ \/ \/ eschaton | cusp \/
-
-== eschaton => google maps on rails with hotness
+== eschaton => google maps on rails for bipeds(and also ungulates)
-Helps with writing google map mashups in Rails bringing together the power of Google Maps and Rails.
+Helps with writing google map mashups in Rails bringing together the Bodacity(measure of bodaciousness) of
+Google Maps and Rails. If you have an issue please feed-it-back on Github issues[http://github.com/yawningman/eschaton/issues]
-Visit the wiki[http://github.com/yawningman/eschaton/wikis] for more info.
+Enough talk, lets code!
- map = Google::Map.new(:center => {:latitude => -33.947, :longitude => 18.462},
- :controls => [:small_map, :map_type])
+ #-----------------------------------------------------------------#
+ map = Google::Map.new(:center => {:latitude => -33.947, :longitude => 18.462},
+ :controls => [:large_map_3D, :map_type])
+
+ # A simple map click
map.click do |script, location|
marker = map.add_marker :location => location
marker.open_info_window :text => 'Awesome, you added a marker!'
end
+
+ #-----------------------------------------------------------------#
+
+ # Create a satellite map, zoomed at 16.
+ map = Google::Map.new(:controls => [:small_map, :map_type],
+ :type => :satellite, :zoom => 16)
-== Get it...
+ # Collect some markers
+ markers = [[-34.2023, 18.3794], [-34.2029, 18.3797], [-34.2022, 18.3811],
+ [-34.2016, 18.3829], [-34.2006, 18.3849]].collect do |location|
+ map.add_marker :location => location
+ end
-For Rails 2.1 and up
+ # ... and draw a line in between them
+ map.add_line :between_markers => markers, :tooltip => {:text => "Short Hike I do everyday with my dog"}
- $ script/plugin install git://github.com/yawningman/eschaton.git
+ #-----------------------------------------------------------------#
+
+ # Draw a polygon, yellow with a green border ..
+ map.add_polygon :vertices => [[-34.2023, 18.3794], [-34.2029, 18.3797], [-34.2022, 18.3811]],
+ :fill_colour => 'yellow', :border_colour => 'green'
+
+ #-----------------------------------------------------------------#
+
+ # A draggable marker that talks when its being dragged and dropped
+ marker = map.add_marker :location => {:latitude => -33.947, :longitude => 18.462},
+ :tooltip => {:text => "Drag me", :show => :always},
+ :draggable => true
-Older versions of Rails
+ marker.when_being_dragged do
+ marker.update_tooltip :text => "Dragging..."
+ end
+
+ marker.when_dropped do |script, drop_location|
+ marker.update_tooltip :text => "Dropped..."
+ marker.open_info_window :text => "Yes, I was getting tired of flying..."
+ end
+
+and always remember...
+
+ {:latitude => -33.947, :longitude => 18.462} == [-33.947, 18.462]
- $ cd vendor/plugins
- $ git clone git://github.com/yawningman/eschaton.git
+Checkout the online[http://yawningman.github.com/eschaton/rdoc/] docs for more general samples.
+
+== Get it...
+
+ $ script/plugin install git://github.com/yawningman/eschaton.git
== Get going...
@@ -42,5 +75,3 @@ Generate and review the docs, put some test code in the <b><i>app/helpers/map_he
$ cd vendor/plugins/eschaton/
$ rake rdoc
$ rake open_doc
-
-You can also view the docs online[http://yawningman.github.com/eschaton/rdoc/]
View
2  lib/eschaton/core_ext/hash.rb
@@ -1,5 +1,7 @@
class Hash # :nodoc:
alias extract delete
+
+ alias has_option? has_key?
# Defaults key values in a hash that are not present. Works like #merge but does not overwrite
# existing keys. This is usefull when using options arguments.
View
32 slices/google_maps/google/bounds.rb
@@ -0,0 +1,32 @@
+module Google
+
+ # Represents a rectangle in geographical coordinates, including one that crosses the 180 degrees meridian.
+ # See googles online[http://code.google.com/apis/maps/documentation/reference.html#GLatLngBounds] docs for details.
+ class Bounds < MapObject
+ attr_reader :south_west_point, :north_east_point
+
+ # ==== Options:
+ # * +south_west_point+ - Optional. The south west point of the rectangle.
+ # * +north_east_point+ - Optional. The north east point of the rectangle.
+ def initialize(options = {})
+ options.default!
+
+ super
+
+ self.south_west_point = Google::OptionsHelper.to_location(options[:south_west_point])
+ self.north_east_point = Google::OptionsHelper.to_location(options[:north_east_point])
+ end
+
+ def to_s
+ "new GLatLngBounds(#{self.south_west_point.to_js}, #{self.north_east_point.to_js})"
+ end
+
+ alias to_js to_s
+ def to_json(options = nil)
+ to_js
+ end
+
+ protected
+ attr_writer :south_west_point, :north_east_point
+ end
+end
View
40 slices/google_maps/google/ground_overlay.rb
@@ -1,31 +1,37 @@
module Google
- # Represents a overlay that can be added to a Map using Map#add_ground_overlay.
+ # Represents a overlay that can be added to a Map using Map#add_ground_overlay. If a method or event is not documented here please
+ # see googles online[http://code.google.com/apis/maps/documentation/reference.html#GGroundOverlay] docs for details.
+ # See MapObject#listen_to on how to use # events not listed on this object.
class GroundOverlay < MapObject
- attr_reader :vertices
+ attr_reader :bounds, :image
+
+ # ==== Options:
+ # * +image+ - Required. The image url that should be used as the overlay.
+ # * +bounds+ - Optional. The bounds that represent the rectangle of the overlay. Can be a Bounds or whatever
+ # Bounds#new supports. If you don't use this option +south_west_point+ and +north_east_point+ can be used explicitly.
+ # * +south_west_point+ - Optional. The south west point of the rectangle.
+ # * +north_east_point+ - Optional. The north east point of the rectangle.
def initialize(options = {})
- options.default! :var => 'ground_overlay',
- :vertices => [],
- :sw => [],
- :ne => [],
- :image_url => ''
+ options.default! :var => :ground_overlay, :image => ''
super
- if create_var?
- sw = options.extract(:sw)
- ne = options.extract(:ne)
- image_url = options.extract(:image_url)
- self.vertices = options.extract(:vertices)
- remaining_options = options
- self.vertices << Google::OptionsHelper.to_location(sw)
- self.vertices << Google::OptionsHelper.to_location(ne)
+ if create_var?
+ self.bounds = if options.has_option?(:bounds)
+ Google::OptionsHelper.to_bounds(options[:bounds])
+ else
+ Google::Bounds.new(:south_west_point => options.extract(:south_west_point),
+ :north_east_point => options.extract(:north_east_point))
+ end
+ self.image = options.extract(:image)
- self << "#{self.var} = new GGroundOverlay('#{image_url}', new GLatLngBounds(new GLatLng(#{sw.join(',')}), new GLatLng(#{ne.join(',')})));"
+ self << "#{self.var} = new GGroundOverlay('#{self.image}', #{self.bounds});"
end
end
+
protected
- attr_writer :vertices
+ attr_writer :bounds, :image
end
end
View
1  slices/google_maps/google/location.rb
@@ -14,6 +14,7 @@ def to_s
"new GLatLng(#{self.latitude}, #{self.longitude})"
end
+ # TODO - Alias this somehow
alias to_js to_s
def to_json(options = nil)
to_js
View
4 slices/google_maps/google/map.rb
@@ -414,7 +414,9 @@ def add_ground_overlay(options)
self.add_overlay ground_overlay
- self.extend_track_bounds ground_overlay.vertices
+ # TODO - Standardise extending bounds with another Bound
+ self.extend_track_bounds ground_overlay.bounds.south_west_point
+ self.extend_track_bounds ground_overlay.bounds.north_east_point
ground_overlay
end
View
10 slices/google_maps/options_helper.rb
@@ -60,6 +60,16 @@ def self.to_location(options)
end
end
+ def self.to_bounds(options)
+ if options.is_a?(Google::Bounds)
+ options
+ elsif options.is_a?(Array)
+ Google::Bounds.new :south_west_point => options.first, :north_east_point => options.second
+ elsif options.is_a?(Hash)
+ Google::Bounds.new options
+ end
+ end
+
# ==== Options:
# * +existing+ - Optional. Indicates if the marker is an existing marker.
def self.to_marker(options)
View
19 test/bounds_test.rb
@@ -0,0 +1,19 @@
+require File.dirname(__FILE__) + '/test_helper'
+
+Test::Unit::TestCase.output_fixture_base = File.dirname(__FILE__)
+
+class BoundsTest < Test::Unit::TestCase
+
+ def test_initialize
+ Eschaton.with_global_script do |script|
+
+ bounds = Google::Bounds.new(:south_west_point => [-34.947, 19.462], :north_east_point => [-35.947, 20.462])
+ bounds_output = "new GLatLngBounds(new GLatLng(-34.947, 19.462), new GLatLng(-35.947, 20.462))"
+
+ assert_equal bounds_output, bounds.to_s
+ assert_equal bounds_output, bounds.to_js
+ assert_equal bounds_output, bounds.to_json
+ end
+ end
+
+end
View
36 test/ground_overlay_test.rb
@@ -0,0 +1,36 @@
+require File.dirname(__FILE__) + '/test_helper'
+
+Test::Unit::TestCase.output_fixture_base = File.dirname(__FILE__)
+
+class GroundOverlayTest < Test::Unit::TestCase
+
+ def test_initialize
+ Eschaton.with_global_script do |script|
+ output = "ground_overlay = new GGroundOverlay('http://battlestar/images/cylon_base_star.png', new GLatLngBounds(new GLatLng(-33.947, 18.462), new GLatLng(-34.947, 19.462)));"
+
+ # Using explicit points for Bounds
+ assert_output_fixture output,
+ script.record_for_test {
+ Google::GroundOverlay.new :image => "http://battlestar/images/cylon_base_star.png",
+ :south_west_point => [-33.947, 18.462],
+ :north_east_point => [-34.947, 19.462]
+ }
+
+ # Using a bounds object
+ assert_output_fixture output,
+ script.record_for_test {
+ Google::GroundOverlay.new :image => "http://battlestar/images/cylon_base_star.png",
+ :bounds => Google::Bounds.new(:south_west_point => [-33.947, 18.462],
+ :north_east_point => [-34.947, 19.462])
+ }
+
+ # using Bounds in array form
+ assert_output_fixture output,
+ script.record_for_test {
+ Google::GroundOverlay.new :image => "http://battlestar/images/cylon_base_star.png",
+ :bounds => [[-33.947, 18.462], [-34.947, 19.462]]
+ }
+ end
+ end
+
+end
View
19 test/location_test.rb
@@ -0,0 +1,19 @@
+require File.dirname(__FILE__) + '/test_helper'
+
+Test::Unit::TestCase.output_fixture_base = File.dirname(__FILE__)
+
+class LocationTest < Test::Unit::TestCase
+
+ def test_initialize
+ Eschaton.with_global_script do |script|
+
+ location = Google::Location.new(:latitude => -34.947, :longitude => 19.462)
+ location_output = "new GLatLng(-34.947, 19.462)"
+
+ assert_equal location_output, location.to_s
+ assert_equal location_output, location.to_js
+ assert_equal location_output, location.to_json
+ end
+ end
+
+end
View
66 test/map_test.rb
@@ -4,6 +4,10 @@
class MapTest < Test::Unit::TestCase
+ def default_test_map
+ Google::Map.new :center => {:latitude => -33.947, :longitude => 18.462}
+ end
+
def test_map_initialize
assert_output_fixture :map_default,
Eschaton.with_global_script{
@@ -27,7 +31,7 @@ def test_map_initialize
def test_add_control
Eschaton.with_global_script do |script|
script.google_map_script do
- map = Google::Map.new :center => {:latitude => -33.947, :longitude => 18.462}
+ map = self.default_test_map
assert_output_fixture 'map.addControl(new GSmallMapControl());',
script.record_for_test {
@@ -70,7 +74,7 @@ def test_add_control
def test_open_info_window_output
Eschaton.with_global_script do |script|
script.google_map_script do
- map = Google::Map.new :center => {:latitude => -33.947, :longitude => 18.462}
+ map = self.default_test_map
# With :url and :include_location params
assert_output_fixture :map_open_info_window_url_center,
@@ -120,7 +124,7 @@ def test_open_info_window_output
def test_update_info_window
Eschaton.with_global_script do |script|
script.google_map_script do
- map = Google::Map.new :center => {:latitude => -33.947, :longitude => 18.462}
+ map = self.default_test_map
assert_output_fixture 'map.openInfoWindow(map.getInfoWindow().getPoint(), "<div id=\'info_window_content\'>" + "Testing text!" + "</div>");',
script.record_for_test {
@@ -133,7 +137,7 @@ def test_update_info_window
def test_click_output
Eschaton.with_global_script do |script|
script.google_map_script do
- map = Google::Map.new :center => {:latitude => -33.947, :longitude => 18.462}
+ map = self.default_test_map
# without body
assert_output_fixture :map_click_no_body,
@@ -163,7 +167,7 @@ def test_click_output
def test_add_marker_output
Eschaton.with_global_script do |script|
script.google_map_script do
- map = Google::Map.new :center => {:latitude => -33.947, :longitude => 18.462}
+ map = self.default_test_map
assert_output_fixture "marker = new GMarker(new GLatLng(-33.947, 18.462), {draggable: false});
map.addOverlay(marker);
@@ -188,7 +192,7 @@ def test_add_marker_output
def test_replace_marker_output
Eschaton.with_global_script do |script|
- map = Google::Map.new :center => {:latitude => -33.947, :longitude => 18.462}
+ map = self.default_test_map
assert_output_fixture "map.removeOverlay(marker);
marker.closeInfoWindow();
@@ -209,7 +213,7 @@ def test_replace_marker_output
def test_change_marker_output
Eschaton.with_global_script do |script|
- map = Google::Map.new :center => {:latitude => -33.947, :longitude => 18.462}
+ map = self.default_test_map
assert_output_fixture "map.removeOverlay(create_spot);
create_spot.closeInfoWindow();
@@ -232,7 +236,7 @@ def test_change_marker_output
def test_add_line
Eschaton.with_global_script do |script|
- map = Google::Map.new :center => {:latitude => -33.947, :longitude => 18.462}
+ map = self.default_test_map
line = map.add_line :vertices => {:latitude => -33.947, :longitude => 18.462}
assert line.is_a?(Google::Line)
@@ -242,7 +246,7 @@ def test_add_line
def test_add_circle
Eschaton.with_global_script do |script|
- map = Google::Map.new :center => {:latitude => -33.947, :longitude => 18.462}
+ map = self.default_test_map
circle = map.add_circle :location => {:latitude => -33.947, :longitude => 18.462}
assert circle.is_a?(Google::Circle)
@@ -252,7 +256,7 @@ def test_add_circle
def test_add_circle_output
Eschaton.with_global_script do |script|
- map = Google::Map.new :center => {:latitude => -33.947, :longitude => 18.462}
+ map = self.default_test_map
add_circle_output = 'circle = drawCircle(new GLatLng(-33.947, 18.462), 1.5, 40, null, 2, null, "#0055ff", null);
map.addOverlay(circle);'
@@ -272,7 +276,7 @@ def test_add_circle_output
def test_add_line_output
Eschaton.with_global_script do |script|
- map = Google::Map.new :center => {:latitude => -33.947, :longitude => 18.462}
+ map = self.default_test_map
assert_output_fixture :map_add_line_with_vertex,
script.record_for_test {
@@ -331,7 +335,7 @@ def test_add_line_output
def test_clear_output
Eschaton.with_global_script do |script|
- map = Google::Map.new :center => {:latitude => -33.947, :longitude => 18.462}
+ map = self.default_test_map
assert_output_fixture 'map.clearOverlays();',
script.record_for_test {
@@ -342,7 +346,7 @@ def test_clear_output
def test_show_map_blowup_output
Eschaton.with_global_script do |script|
- map = Google::Map.new :center => {:latitude => -33.947, :longitude => 18.462}
+ map = self.default_test_map
# Default with hash location
assert_output_fixture 'map.showMapBlowup(new GLatLng(-33.947, 18.462), {});',
@@ -382,7 +386,7 @@ def test_show_map_blowup_output
def test_remove_type
Eschaton.with_global_script do |script|
- map = Google::Map.new :center => {:latitude => -33.947, :longitude => 18.462}
+ map = self.default_test_map
assert_output_fixture 'map.removeMapType(G_SATELLITE_MAP);',
script.record_for_test {
@@ -428,7 +432,7 @@ def test_best_fit_center_and_zoom
def test_auto_zoom
Eschaton.with_global_script do |script|
- map = Google::Map.new :center => {:latitude => -33.947, :longitude => 18.462}
+ map = self.default_test_map
assert_output_fixture 'map.setZoom(map.getBoundsZoomLevel(track_bounds));',
script.record_for_test {
@@ -439,7 +443,7 @@ def test_auto_zoom
def test_auto_center
Eschaton.with_global_script do |script|
- map = Google::Map.new :center => {:latitude => -33.947, :longitude => 18.462}
+ map = self.default_test_map
assert_output_fixture 'if(!track_bounds.isEmpty()){
map.setCenter(track_bounds.getCenter());
@@ -452,7 +456,7 @@ def test_auto_center
def test_auto_fit
Eschaton.with_global_script do |script|
- map = Google::Map.new :center => {:latitude => -33.947, :longitude => 18.462}
+ map = self.default_test_map
assert_output_fixture 'map.setZoom(map.getBoundsZoomLevel(track_bounds));
if(!track_bounds.isEmpty()){
@@ -464,4 +468,32 @@ def test_auto_fit
end
end
+ def test_add_ground_overlay
+ Eschaton.with_global_script do |script|
+ map = self.default_test_map
+ output = "ground_overlay = new GGroundOverlay('http://battlestar/images/cylon_base_star.png', new GLatLngBounds(new GLatLng(-33.947, 18.462), new GLatLng(-34.947, 19.462)));
+ map.addOverlay(ground_overlay);
+ track_bounds.extend(new GLatLng(-33.947, 18.462));
+ track_bounds.extend(new GLatLng(-34.947, 19.462));"
+
+ ground_overlay_options = {:image => "http://battlestar/images/cylon_base_star.png",
+ :south_west_point => [-33.947, 18.462],
+ :north_east_point => [-34.947, 19.462]}
+
+ # TODO - This is a hack, see Github issue http://github.com/yawningman/eschaton/issues/#issue/1
+ ground_overlay_other = ground_overlay_options.clone
+
+ assert_output_fixture output,
+ script.record_for_test {
+ map.add_ground_overlay ground_overlay_options
+ }
+
+ assert_output_fixture output,
+ script.record_for_test {
+ map.add_ground_overlay Google::GroundOverlay.new(ground_overlay_other)
+ }
+
+ end
+ end
+
end
View
59 test/options_helper_test.rb
@@ -43,7 +43,7 @@ def test_options_helper_to_location
location_hash = {:latitide => 34, :longitude => 18}
location = Google::OptionsHelper.to_location(location_hash)
- assert_equal Google::Location, location.class
+ assert location.is_a?(Google::Location)
assert_equal location_hash[:latitude], location.latitude
assert_equal location_hash[:longitude], location.longitude
@@ -97,7 +97,7 @@ def test_to_encoded_polylines
end
def test_to_content
- assert_equal 'the mystic', Google::OptionsHelper.to_content(:text => 'the mystic')
+ assert_equal 'the mystic', Google::OptionsHelper.to_content(:text => 'the mystic')
assert_equal 'test output for render', Google::OptionsHelper.to_content(:partial => 'testing')
Eschaton.with_global_script do |script|
@@ -108,4 +108,59 @@ def test_to_content
end
end
+ def test_to_bounds
+ south_west_point_latitude, south_west_point_longitude = -34.947, 19.462
+ north_east_point_latitude, north_east_point_longitude = -35.947, 20.462
+
+ bounds = Google::Bounds.new(:south_west_point => [south_west_point_latitude, south_west_point_longitude],
+ :north_east_point => [north_east_point_latitude, north_east_point_longitude])
+
+ assert_equal bounds, Google::OptionsHelper.to_bounds(bounds)
+ assert_equal south_west_point_latitude, bounds.south_west_point.latitude
+ assert_equal south_west_point_longitude, bounds.south_west_point.longitude
+ assert_equal north_east_point_latitude, bounds.north_east_point.latitude
+ assert_equal north_east_point_longitude, bounds.north_east_point.longitude
+
+ bounds_from_hash = Google::OptionsHelper.to_bounds(:south_west_point => [south_west_point_latitude, south_west_point_longitude],
+ :north_east_point => [north_east_point_latitude, north_east_point_longitude])
+ assert bounds_from_hash.is_a?(Google::Bounds)
+ assert_equal south_west_point_latitude, bounds_from_hash.south_west_point.latitude
+ assert_equal south_west_point_longitude, bounds_from_hash.south_west_point.longitude
+ assert_equal north_east_point_latitude, bounds_from_hash.north_east_point.latitude
+ assert_equal north_east_point_longitude, bounds_from_hash.north_east_point.longitude
+
+ bounds_from_array = Google::OptionsHelper.to_bounds([[south_west_point_latitude, south_west_point_longitude],
+ [north_east_point_latitude, north_east_point_longitude]])
+ assert bounds_from_hash.is_a?(Google::Bounds)
+ assert_equal south_west_point_latitude, bounds_from_array.south_west_point.latitude
+ assert_equal south_west_point_longitude, bounds_from_array.south_west_point.longitude
+ assert_equal north_east_point_latitude, bounds_from_array.north_east_point.latitude
+ assert_equal north_east_point_longitude, bounds_from_array.north_east_point.longitude
+ end
+
+ def test_to_ground_overlay
+ image = 'http://battlestar/images/cylon_base_star.png'
+ south_west_point_latitude, south_west_point_longitude = -34.947, 19.462
+ north_east_point_latitude, north_east_point_longitude = -35.947, 20.462
+
+ ground_overlay = Google::GroundOverlay.new(:image => image,
+ :south_west_point => [south_west_point_latitude, south_west_point_longitude],
+ :north_east_point => [north_east_point_latitude, north_east_point_longitude])
+
+ assert_equal ground_overlay, Google::OptionsHelper.to_ground_overlay(ground_overlay)
+
+ ground_overlay_from_hash = Google::OptionsHelper.to_ground_overlay(:image => image,
+ :south_west_point => [south_west_point_latitude,
+ south_west_point_longitude],
+ :north_east_point => [north_east_point_latitude,
+ north_east_point_longitude])
+
+ assert ground_overlay_from_hash.is_a?(Google::GroundOverlay)
+ assert_equal 'http://battlestar/images/cylon_base_star.png', ground_overlay_from_hash.image
+ assert_equal south_west_point_latitude, ground_overlay_from_hash.bounds.south_west_point.latitude
+ assert_equal south_west_point_longitude, ground_overlay_from_hash.bounds.south_west_point.longitude
+ assert_equal north_east_point_latitude, ground_overlay_from_hash.bounds.north_east_point.latitude
+ assert_equal north_east_point_longitude, ground_overlay_from_hash.bounds.north_east_point.longitude
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.