ym4r 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README +16 -8
- data/lib/ym4r/google_maps/map.rb +12 -7
- data/lib/ym4r/google_maps/mapping.rb +12 -4
- data/lib/ym4r/google_maps/overlay.rb +2 -2
- data/rakefile.rb +1 -1
- data/test/test_google_maps.rb +19 -0
- metadata +4 -4
- data/test/test_maps.rb +0 -14
data/README
CHANGED
@@ -8,18 +8,29 @@ You can use the library to display Google maps easily with any ruby-based web fr
|
|
8
8
|
- http://thepochisuperstarmegashow.com/2006/06/03/google-maps-yahoo-traffic-mash-up/
|
9
9
|
Following is some notes about using the library:
|
10
10
|
|
11
|
+
=====Naming conventions
|
12
|
+
The names of the Ruby class follow the ones in the JavaScript Google Maps API v2, except for GMap2, which in Ruby code is called simply GMap. To know what is possible to do with each class, you should refer to the documentation available on Google website.
|
13
|
+
|
14
|
+
On top of that, you have some convenience methods for initializing the map (in the GMap class). Also, the constructors of some classes accept different types of arguments to be converted later in the correct JavaScript format. For example, the +GMarker+ aclass accepts an array of 2 floats as parameter, in addition of a GLatLng object, to indicate its position. It also facilitates the attribution of an HTML info window, displayed when the user clicks on it, since you can pass to the constructor an options hash with the <tt>:info_window</tt> key and the text to display as the value, instead of having to wire the response to a click event yourself.
|
15
|
+
|
11
16
|
=====Binding JavaScript and Ruby
|
12
17
|
Since the Google Maps API uses JavaScript to create and manipulate a map, most of what the library does is outputting JavaScript, although some convenience methods are provided to simplify some common operations at initialization time. When you create a YM4R mapping object (a Ruby object which includes the MappingObject module) and call methods on it, these calls are converted by the library into JavaScript code. At initialization time, you can pass arbitrary JavaScript code to the <tt>GMap#record_init</tt> and <tt>GMap#record_global_init</tt>.Then, at update time, if you use Ruby-on-Rails as your web framework, you can update your map through RJS by passing the result of the method calls to the <tt>page.send :record,...</tt> method to have it then interpreted by the browser.
|
13
18
|
|
14
19
|
For example, here is a typical initialization sequence for a map
|
15
20
|
@map = GMap.new("map_div")
|
21
|
+
@map.control_init(:large_map => true,:map_type => true)
|
16
22
|
@map.center_zoom_init([35.12313,-110.567],12)
|
17
23
|
@map.record_init @map.add_overlay(GMarker.new([35.12878, -110.578],:title => "Hello!"))
|
18
24
|
|
19
|
-
While +center_zoom_init+
|
25
|
+
While +center_zoom_init+ and +control_init+ are one of the rare convenience methods that do not output JavaScript, the +add_overlay+ does. Actually, if you look at the code of the GMap class, you won't find any +add_overlay+ method, although in the documentation of the GMap2 class from the Google Maps API documentation, you will find something about the +addOverlay+ JavaScript method. In fact, when you call on a mapping object an unknow method, it is converted to a javascriptified version of it, along with its arguments, and a string of JavaScript code is output. So the <tt>@map.add_overlay...</tt> above is converted to <tt>"map.addOverlay(new GMarker(GLatLng.new(35.12878, -110.578),{title:\"Hello!\"}))"</tt>, which is then passed to the +record_init+ method of a Ruby GMap object to be later output along with the rest of the initialization code.
|
26
|
+
|
27
|
+
Starting with version 0.1.4 of the YM4R library, this call to +record_init+ to add an overlay could have been replaced with a call to +overlay_init+:
|
28
|
+
@map.overlay_init GMarker.new([35.12878, -110.578],:title => "Hello!")
|
29
|
+
which is strictly equivalent to
|
30
|
+
@map.record_init @map.add_overlay(new GMarker([35.12878, -110.578],:title => "Hello!"))
|
20
31
|
|
21
32
|
=====Initialization of the map
|
22
|
-
The map is represented by a GMap object. You need to pass to the constructor the id of a DIV that will contain the map. You have to place this DIV yourself in your HTML template. You can also optionnally pass to the constructor the JavaScript name of the variable that will reference the map, which by default will be global in JavaScript. You have convenience methods to setup the controls, the center, the zoom and the icons (which are also global). You can also pass arbitrary JavaScript to +record_init+ and +record_global_init+. Since, by default, the initialization of the map is performed in a callback function, if you want to have a globally accessible variable, you need to use the +global+ version.
|
33
|
+
The map is represented by a GMap object. You need to pass to the constructor the id of a DIV that will contain the map. You have to place this DIV yourself in your HTML template. You can also optionnally pass to the constructor the JavaScript name of the variable that will reference the map, which by default will be global in JavaScript. You have convenience methods to setup the controls, the center, the zoom, overlays and the icons (which are also global). You can also pass arbitrary JavaScript to +record_init+ and +record_global_init+. Since, by default, the initialization of the map is performed in a callback function, if you want to have a globally accessible variable, you need to use the +global+ version.
|
23
34
|
|
24
35
|
Then in your template, you have 2 necessary calls:
|
25
36
|
- <tt>GMap#header</tt>: Outputs the inclusion of the JavaScript file from Google to make use of the Google Maps API + a style declaration for VML objects, necessary to display polylines under IE.
|
@@ -33,12 +44,9 @@ You are able to update the map through Ajax. For example, in Ruby-on-Rails, you
|
|
33
44
|
For example, if you want to add a marker to the map, you need to do a few things. First, you have to bind a Ruby mapping object to the global JavaScript map variable. By default its name is +map+, but you could have overriden that at initialization time. You need to do something like this:
|
34
45
|
@map = Variable.new("map")
|
35
46
|
+map+ in the Variable constructor is the name of the global JavaScript map variable. Then any method you call on <tt>@map</tt> will be converted in JavaScript to a method called on +map+. In your RJS code, you would do something like this to add a marker:
|
36
|
-
page.send :record, @map.add_overlay(GMarker.new([123123.1,12313.76],:
|
37
|
-
|
38
|
-
|
39
|
-
The names of the Ruby class follow the ones in the JavaScript Google Maps API v2. To know what is possible to do, you should refer to the documentation available on Google website.
|
40
|
-
|
41
|
-
On top of that, you have some convenience methods for initializing the map (in the GMap class). Also, the constructors of some classes accept different types of arguments to be converted later in the correct JavaScript format. For example, the +GMarker+ aclass accepts an array of 2 floats as parameter, in addition of a GLatLng object, to indicate its position. It also facilitates the attribution of an HTML info window, displayed when the user clicks on it, since you can pass to the constructor an options hash with the <tt>:info_window</tt> key and the text to display as the value.
|
47
|
+
page.send :record, @map.add_overlay(GMarker.new([123123.1,12313.76],:title => "Hello again!"))
|
48
|
+
What is sent to the browser will be the fllowing JavaScript code:
|
49
|
+
map.addOverlay(new GMarker(new GLatLng(123123.1,12313.76),{title:\"Hello again!\"}))
|
42
50
|
|
43
51
|
====Yahoo Maps Building Block API
|
44
52
|
Building Block API's (Geocoding, Map Image, Traffic and Local Search) are supported. You have to pass to the +get+ method of the module a hash whose keys are a rubyfied version of the request parameters detailed in the documentation for these API's. You get back a ruby object, with accessors that let you get the returned data in a easy way. You get an exception if not all the parameters have been passed, if the connection to the service could not be made or if the parameters you have passed are of the incorrect value.
|
data/lib/ym4r/google_maps/map.rb
CHANGED
@@ -14,8 +14,8 @@ module Ym4r
|
|
14
14
|
def initialize(container, variable = "map")
|
15
15
|
@container = container
|
16
16
|
@variable = variable
|
17
|
-
@init =
|
18
|
-
@global_init =
|
17
|
+
@init = []
|
18
|
+
@global_init = []
|
19
19
|
end
|
20
20
|
|
21
21
|
#Outputs the header necessary to use the Google Maps API. By default, it also outputs a style declaration for VML elements.
|
@@ -54,6 +54,11 @@ module Ym4r
|
|
54
54
|
end
|
55
55
|
end
|
56
56
|
|
57
|
+
#Initializes the map by adding an overlay (marker or polyline). It can be called multiple times
|
58
|
+
def overlay_init(overlay)
|
59
|
+
@init << add_overlay(overlay)
|
60
|
+
end
|
61
|
+
|
57
62
|
#Records arbitrary JavaScript code and outputs it during initialization outside the +load+ function (ie globally).
|
58
63
|
def record_global_init(code)
|
59
64
|
@global_init << code
|
@@ -76,16 +81,16 @@ module Ym4r
|
|
76
81
|
html << "<script type=\"text/javascript\">\n" if !no_script_tag
|
77
82
|
html << "function addInfoWindowToMarker(marker,info){\nGEvent.addListener(marker, \"click\", function() {\nmarker.openInfoWindowHtml(info);\n});\nreturn marker;\n}\n"
|
78
83
|
html << "function addInfoWindowTabsToMarker(marker,info){\nGEvent.addListener(marker, \"click\", function() {\nmarker.openInfoWindowTabsHtml(info);\n});\nreturn marker;\n}\n"
|
79
|
-
html << @global_init
|
84
|
+
html << @global_init * "\n"
|
80
85
|
html << "var #{@variable};\n" if !no_declare and !no_global
|
81
86
|
html << "function #{load_method}() {\nif (GBrowserIsCompatible()) {\n" if !no_load
|
82
87
|
if !no_declare and no_global
|
83
|
-
html << declare(@variable)
|
88
|
+
html << "#{declare(@variable)}\n"
|
84
89
|
else
|
85
|
-
html << assign_to(@variable)
|
90
|
+
html << "#{assign_to(@variable)}\n"
|
86
91
|
end
|
87
|
-
html << @init
|
88
|
-
html << "}\n}\n" if !no_load
|
92
|
+
html << @init * "\n"
|
93
|
+
html << "\n}\n}\n" if !no_load
|
89
94
|
html << "</script>" if !no_script_tag
|
90
95
|
html
|
91
96
|
end
|
@@ -10,7 +10,7 @@ module Ym4r
|
|
10
10
|
args.collect! do |arg|
|
11
11
|
javascriptify_variable(arg)
|
12
12
|
end
|
13
|
-
"#{to_javascript}.#{javascriptify_method(name.to_s)}(#{args.join(",")})
|
13
|
+
Variable.new("#{to_javascript}.#{javascriptify_method(name.to_s)}(#{args.join(",")})")
|
14
14
|
end
|
15
15
|
|
16
16
|
#Transforms a Ruby object into a JavaScript string
|
@@ -37,13 +37,13 @@ module Ym4r
|
|
37
37
|
#Declares a Mapping Object bound to a JavaScript variable of name +variable+.
|
38
38
|
def declare(variable)
|
39
39
|
@variable = variable
|
40
|
-
"var #{variable} = #{create}
|
40
|
+
"var #{variable} = #{create};"
|
41
41
|
end
|
42
42
|
|
43
43
|
#Binds a Mapping object to a previously declared JavaScript variable of name +variable+.
|
44
44
|
def assign_to(variable)
|
45
45
|
@variable = variable
|
46
|
-
"#{variable} = #{create}
|
46
|
+
"#{variable} = #{create};"
|
47
47
|
end
|
48
48
|
|
49
49
|
#Returns a Javascript code representing the object
|
@@ -61,12 +61,20 @@ module Ym4r
|
|
61
61
|
end
|
62
62
|
end
|
63
63
|
|
64
|
-
#Used to bind a ruby variable to an already existing JavaScript one.
|
64
|
+
#Used to bind a ruby variable to an already existing JavaScript one. IT doesn't have to be a variable in the sense "var variable" but it can be any valid JavaScript expression that has a value.
|
65
65
|
class Variable
|
66
66
|
include MappingObject
|
67
67
|
def initialize(variable)
|
68
68
|
@variable = variable
|
69
69
|
end
|
70
|
+
#Returns the javascript expression contained in the object.
|
71
|
+
def create
|
72
|
+
@variable
|
73
|
+
end
|
74
|
+
#Returns the expression inside the Variable followed by a ";"
|
75
|
+
def to_s
|
76
|
+
@variable + ";"
|
77
|
+
end
|
70
78
|
end
|
71
79
|
end
|
72
80
|
end
|
@@ -68,9 +68,9 @@ module Ym4r
|
|
68
68
|
end
|
69
69
|
#Declares a GIcon. It is necessary to declare an icon before using it, since it is the only way to set up its attributes.
|
70
70
|
def declare(variable)
|
71
|
-
decl = super(variable)
|
71
|
+
decl = super(variable) + "\n"
|
72
72
|
@options.each do |key,value|
|
73
|
-
decl << "#{
|
73
|
+
decl << "#{to_javascript}.#{javascriptify_method(key.to_s)} = #{javascriptify_variable(value)};\n"
|
74
74
|
end
|
75
75
|
decl
|
76
76
|
end
|
data/rakefile.rb
CHANGED
@@ -0,0 +1,19 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
2
|
+
|
3
|
+
require 'ym4r/google_maps'
|
4
|
+
require 'test/unit'
|
5
|
+
|
6
|
+
include Ym4r::GoogleMaps
|
7
|
+
|
8
|
+
class TestGoogleMaps< Test::Unit::TestCase
|
9
|
+
def test_js_export
|
10
|
+
map = GMap.new("map_div")
|
11
|
+
var = Variable.new("hello")
|
12
|
+
yuo = Variable.new("salam")
|
13
|
+
poi = Variable.new("poi")
|
14
|
+
map.record_init map.add_overlay(GMarker.new([123.5,123.56]))
|
15
|
+
map.record_init map.dummy_method(var.other_dummy_method(yuo.kaka_boudin),poi)
|
16
|
+
map.control_init(:small_map => true)
|
17
|
+
puts map.to_html
|
18
|
+
end
|
19
|
+
end
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
|
|
3
3
|
specification_version: 1
|
4
4
|
name: ym4r
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.1.
|
7
|
-
date: 2006-06-
|
6
|
+
version: 0.1.4
|
7
|
+
date: 2006-06-05 00:00:00 +05:00
|
8
8
|
summary: Using Google Maps and Yahoo! Maps from Ruby and Rails
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -55,8 +55,8 @@ files:
|
|
55
55
|
- lib/ym4r/yahoo_maps/flash/widget.rb
|
56
56
|
- lib/ym4r/google_maps/config/config.yml
|
57
57
|
- test/test_geocoding.rb
|
58
|
+
- test/test_google_maps.rb
|
58
59
|
- test/test_local_search.rb
|
59
|
-
- test/test_maps.rb
|
60
60
|
- test/test_map_image.rb
|
61
61
|
- test/test_traffic.rb
|
62
62
|
- README
|
@@ -64,8 +64,8 @@ files:
|
|
64
64
|
- rakefile.rb
|
65
65
|
test_files:
|
66
66
|
- test/test_geocoding.rb
|
67
|
+
- test/test_google_maps.rb
|
67
68
|
- test/test_local_search.rb
|
68
|
-
- test/test_maps.rb
|
69
69
|
- test/test_map_image.rb
|
70
70
|
- test/test_traffic.rb
|
71
71
|
rdoc_options:
|