ym4r_gm 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ gem "ym4r"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "shoulda", ">= 0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.5.1"
12
+ gem "rcov", ">= 0"
13
+ end
@@ -0,0 +1,22 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.5.1)
6
+ bundler (~> 1.0.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rake (0.8.7)
10
+ rcov (0.9.9)
11
+ shoulda (2.11.3)
12
+ ym4r (0.6.1)
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ bundler (~> 1.0.0)
19
+ jeweler (~> 1.5.1)
20
+ rcov
21
+ shoulda
22
+ ym4r
@@ -0,0 +1,467 @@
1
+ # YM4R/GM plugin for Rails 3
2
+ Note by guilleiguaran: If you need the version for Rails 2.x check this repo: <a href="http://github.com/ewildgoose/ym4r_gm">http://github.com/ewildgoose/ym4r_gm</a>
3
+
4
+ This is the latest version of the YM4R/GM plugin for Rails (YM4RGMP4R?). Its aim is to facilitate the use of Google Maps from Rails application. It includes and enhances all the web application-related functionalities found in the YM4R gem as of version 0.4.1.
5
+
6
+
7
+ ## Getting Started
8
+ I present here the most common operations you would want to do with YM4R/GM. Read the rest of the documents if you want more details.
9
+
10
+ In your controller, here is a typical initialization sequence in action *index*:
11
+
12
+
13
+ > def index
14
+ > @map = GMap.new("map_div")
15
+ > @map.control_init(:large_map => true,:map_type => true)
16
+ > @map.center_zoom_init([75.5,-42.56],4)
17
+ > @map.overlay_init(GMarker.new([75.6,-42.467],:title => "Hello", :info_window => "Info! Info!"))
18
+ > end
19
+
20
+ Here I create a GMap (which will be placed inside a DIV of id *map_div*), add controls (large zoom slider and pan cross + map type selector), set the center and the zoom and add a marker. Of these 4 steps only the creation of the map and the setting of the center and the zoom are absolutely necessary.
21
+
22
+ In your view, here is what you would have:
23
+
24
+ > <html><head><title>Test</title>
25
+ > <%= raw(GMap.header) %>
26
+ > <%= raw(@map.to_html) %>
27
+ > </head><body>
28
+ > <%= raw(@map.div(:width => 600, :height => 400)) %>
29
+ > </body></html>
30
+
31
+ First you must output the header, used by all the maps of the page: It includes the Google Maps API JS code and the JS helper functions of YM4R/GM. Then we initialize the map by calling *@map.to_html*. In the body, we need a DIV whose +id+ is the same as the one passed to the GMap constructor in the controller. The call to *@map.div* outputs such a DIV. We pass to it options to set the width and height of the map in the style attribute of the DIV.
32
+
33
+ **Note that you need to set a size for the map DIV element at some point or the map will not be displayed.** You have a few ways to do this:
34
+
35
+ * You define it yourself, wherever you want. Usually as part of the layout definition in an external CSS.
36
+ * In the head of the document, through a CSS instruction output by *@map.header_width_height*, to which you pass 2 arguments (width and height).
37
+ * When outputting the DIV with *@map.div*, you can also pass an option hash, with keys *:width* and *:height* and a style attribute for the DIV element will be output.
38
+
39
+ You can update the map trough RJS. Here is an action you can call from a *link_remote_tag* which would do this:
40
+
41
+ > def update
42
+ > @map = Variable.new("map")
43
+ > @marker = GMarker.new([75.89,-42.767],:title => "Update", :info_window => "I have been placed through RJS")
44
+ > end
45
+
46
+ Here, I first bind the Ruby *@map* variable to the JS variable *map*, which already exists in the client browser. +map+ is by default the name given to a map created from YM4R/GM (this could be overriden by passing a second argument to the GMap constructor). Then I create a marker.
47
+
48
+ And you would have inside the RJS template for the action:
49
+ > page << raw(@map.clear_overlays)
50
+ > page << raw(@map.add_overlay(@marker))
51
+
52
+ Here I first clear the map of all overlays. Then I add the marker. Note that the +overlay_init+ is not used anymore since, as its name indicates, this method is only for the initialization of the map.
53
+
54
+
55
+ ## Relation between the YM4R gem and the YM4R/GM plugin
56
+ They are completely independent from each other.
57
+
58
+ With the plugin, you don't need the YM4R gem anymore, unless you want to use the tilers or the Ruby helpers for the Yahoo! Maps Building Block API's and the Google Maps geocoding API. Please refer to the documentation of the YM4R gem to know more about the functionalities in it.
59
+
60
+ Conversely, the YM4R gem does not need the plugin to work.
61
+
62
+
63
+ ## Installation
64
+ Install like any other Rails plugin:
65
+ rails plugin install git://github.com/guilleiguaran/ym4r_gm.git
66
+
67
+ As part of the installation procedure, the JavaScript files found in the *PLUGIN_ROOT/javascript* directory will be copied to the *Rails.root/public/javascripts/* directory.
68
+
69
+ Moreover a *gmaps_api_key.yml* file will also be created in the *RAILS_ROOT/config/* folder. If this file already exists (installed for example by a previous version of the plugin), nothing will be done to it, so you can keep your configuration data even after updating the plugin. This file is a YAML representation of a hash, similar to the *database.yml* file in that the primary keys in the file are the different environments (development, test, production), since you will probably need to have different Google Maps API keys depending on that criteria (for example: a key for localhost for development and test; a key for your host for production). If you don't have any special need, there will be only one key associated with each environment. If however, you need to have multiple keys (for example if your app will be accessible from multiple URLs, for which you need different keys), you can also associate a hash to the environment, whose keys will be the different hosts. In this case, you will need to pass a value to the *:host* key when calling the method *GMap.header* (usually *@request.host*).
70
+
71
+
72
+ ## Migration from the YM4R gem versions <= 0.4.1
73
+ Apart from the installation of the plugin detailed above, you will also need to delete the instructions to require the file and include the Ym4r::GoogleMaps module in your controllers:
74
+ require 'ym4r'
75
+ include Ym4r::GoogleMaps
76
+ This lines are now not needed since the plugin is loaded as part of the normal Rails loading procedure and the module is included when the plugin is loaded.
77
+
78
+
79
+ ## Operations
80
+ You can use the library to display Google maps easily from Rails. The version of the API used is v2. The library is engineered so updates to the map through RJS/Ajax are possible. I have made available 2 in-depth tutorials to show some of the functionalities of the library and how it can be integrated with GeoRuby and the Spatial Adapter for Rails:
81
+
82
+ * http://thepochisuperstarmegashow.com/2006/06/02/ym4r-georuby-spatial-adapter-demo/
83
+ * http://thepochisuperstarmegashow.com/2006/06/03/google-maps-yahoo-traffic-mash-up/
84
+
85
+ Following is some notes about manipulating Google Maps with the library:
86
+
87
+
88
+ ### Naming conventions
89
+ 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.
90
+
91
+ 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 *:info_window* key and the text to display as the value, instead of having to wire the response to a click event yourself.
92
+
93
+ ### Binding JavaScript and Ruby
94
+ 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 *GMap#record_init* and *GMap#record_global_init*.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 *page << * method to have it then interpreted by the browser.
95
+
96
+ For example, here is a typical initialization sequence for a map
97
+
98
+ > @map = GMap.new("map_div")
99
+ > @map.control_init(:large_map => true,:map_type => true)
100
+ > @map.center_zoom_init([35.12313,-110.567],12)
101
+ > @map.overlay_init GMarker.new([35.12878, -110.578],:title => "Hello!")
102
+ > @map.record_init @map.add_overlay(GMarker.new([35.12878, -110.578],:title => "Hello!"))
103
+
104
+ While *center_zoom_init*, *control_init* or *overlay_init* (and generally all the GMap methods which end in *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 *@map.add_overlay...* above is converted to *"map.addOverlay(new GMarker(GLatLng.new(35.12878, -110.578),{title:\"Hello!\"}))"*, 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. Any arbitrary JavaScript code can be passed to the +record_init+ method. Note that 2 last lines of the previous code sample are strictly equivalent and since the +overlay_init+ version is a bit simpler, it should be preferred.
105
+
106
+
107
+ ### Initialization of the map
108
+ 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, the interface configuration (continuous zoom, double click zoom, dragging), map types 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.
109
+
110
+ You can also have multiple maps. Just make sure you give them different DIV id's, as well as different global variable names, when constructing them:
111
+
112
+ > @map1 = GMap("map1_div","map1")
113
+ > @map2 = GMap("map2_div","map2")
114
+
115
+ The other absolutely necessary initialization step in the controller is the setting of center and zoom:
116
+
117
+ > @map1.center_zoom_init([49.12,-56.453],3)
118
+
119
+ Without this code the map will display an empty grey rectangle with Google's logo.
120
+
121
+ Then in your template, you have 2 necessary calls:
122
+
123
+ * The static *GMap.header*: Outputs the inclusion of the JavaScript file from Google to make use of the Google Maps API and by default a style declaration for VML objects, necessary to display polylines under IE. This default can be overriddent by passing *:with_vml => false* as option to the +header+ method. You can also pass to this method a *:host* option in order to select the correct key for the location where your app is currently deployed, in case the current environment has multiple possible keys. Usually, in this case, you should pass it *@request.host*. Finally you can override all the key settings in the configuration by passing a value to the *:key* key.
124
+
125
+ * *GMap#to_html*: Outputs the initialization code of the map. By default, it outputs the +script+ tags and initializes the map in response to the onload event of the JavaScript window object. You can call +to_html+ on each one of your maps to have them all initialized. You can pass the option *:full=>true* to the method to setup a fullscreen map, which will also be resized when the window size changes.
126
+
127
+ You can also use *GMap#div* to output the div element with the correct +id+. You can pass it options *:height* and *:width* to set the size of the div (although, as indicated below, you have other ways to do that).
128
+
129
+ So you should have something like the following:
130
+ > <html><head><title>Hello</title>
131
+ > <%= raw(GMap.header) %>
132
+ > <%= raw(@map.to_html) %>
133
+ > </head>
134
+ > <body><%= raw(@map.div(:width => 600,:height => 400)) %></body>
135
+ > </html>
136
+
137
+ **Note that you need to set a size for the map DIV element at some point or the map will not be displayed.** You have a few ways to do this:
138
+
139
+ * You define it yourself, wherever you want. Usually as part of the layout definition in an external CSS.
140
+ * In the head of the document, through a CSS instruction output by *@map.header_width_height*, to which you pass 2 arguments (width and height).
141
+ * When outputting the DIV with *@map.div*, you can also pass an option hash, with keys *:width* and *:height* and a style attribute for the DIV element will be output.
142
+
143
+ ### GMarkers
144
+ GMarkers are point of interests on a map. You can give a position to a GMarker constructor either by passing it a 2D-array of coordinates, a GLatLng object, an object of type Variable (which evaluates to a GLatLng when interpreted in the browser) or an address, which will be geocoded when the marker is initialized by the map.
145
+
146
+ You can pass options to the GMarker to customize the info window (*:info_window* or *:info_window_tabs* options), the tooltip (*:title* option) or the icon used (*:icon* option).
147
+
148
+ For example:
149
+ > GMarker.new([12.4,65.6],:info_window => "**I'm a Popup window**",:title => "Tooltip")
150
+ > GMarker.new(GLatLng.new([12.3,45.6]))
151
+ > GMarker.new("Rue Clovis Paris France", :title => "geocoded")
152
+
153
+
154
+ ### Update of the map
155
+ You are able to update the map through Ajax. In Ruby-on-Rails, you have something called RJS, which sends JavaScript created on the server as a response to an action, which is later interpreted by the browser. It is usually used for visual effects and replacing or adding elements. It can also accept arbitrary JavaScript and this is what YM4R uses.
156
+
157
+ 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:
158
+
159
+ > @map = Variable.new("map")
160
+
161
+ *map* in the Variable constructor is the name of the global JavaScript map variable. Then any method you call on *@map* 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:
162
+
163
+ > page << raw(@map.add_overlay(GMarker.new([12.1,12.76],:title => "Hello again!")))
164
+
165
+ What is sent to the browser will be the fllowing JavaScript code:
166
+
167
+ > map.addOverlay(new GMarker(new GLatLng(123123.1,12313.76),{title:\"Hello again!\"}))
168
+
169
+
170
+ ### GPolyline
171
+ GPolylines are colored lines on the map. The constructor takes as argument a list of GLatLng or a list of 2-element arrays, which will be transformed into GLatLng for you. It can also take the color (in the *#rrggbb* form), the weight (an integer) and the opacity. These arguments are optional though.
172
+
173
+ For example:
174
+ > GPolyline.new([[12.4,65.6],[4.5,61.2]],"#ff0000",3,1.0)
175
+
176
+ Then you add it like any other overlay:
177
+ > @map.overlay_init(polyline)
178
+
179
+ ### GPolygon
180
+ GPolygons are colored areas on the map. As of 29/12, this feature is not documented in the official GMaps API, but thanks to Steven Talcott Smith (http://www.talcottsystems.com), it is possible to use it now in ym4r.
181
+
182
+ The constructor takes as argument a list of GLatLng or a list of 2-element arrays, which will be transformed into GLatLng for you. Note that for polygons, the last point must be equal to the first, in order to have a closed loop. It can also take the color (in the *#rrggbb* form) of the stroke, the weight of the stroke, the opacity of the stroke, as well as the color of the fill and the opacity. These arguments are optional though.
183
+
184
+ For example:
185
+
186
+ > GPolygon.new([[12.4,6.6],[4.5,1.2],[-5.6,-12.4],[12.4,6.6]],"#ff0000",3,1.0,"#00ff00",1.0)
187
+
188
+ Then you add it like any other overlay:
189
+
190
+ > @map.overlay_init(polygon)
191
+
192
+ ### GMarkerGroup
193
+ A new type of GOverlay is available, called GMarkerGroup.
194
+
195
+ **To use it you would have to include in your HTML template the JavaScript file *markerGroup.js* after the call to *GMap.header* (because it extends the GOverlay class).** You should have something like that in your template:
196
+
197
+ > <%= javascript_include_tag("markerGroup") %>
198
+
199
+ It is useful in 2 situations:
200
+
201
+ * Display and undisplay a group of markers without referencing all of them. You just declare your marker group globally and call +activate+ and +deactivate+ on this group in response, for example, to clicks on links on your page.
202
+
203
+ * Keeping an index of markers, for example, in order to show one of these markers in reponse to a click on a link (the way Google Local does with the results of the search).
204
+
205
+ Here is how you would use it from your Ruby code:
206
+
207
+ > @map = GMap.new("map_div")
208
+ > marker1 = GMarker.new([123.55,123.988],:info_window => "Hello from 1!")
209
+ > marker2 = GMarker.new([87.123,18.9],:info_window =>"Hello from 2!")
210
+ > @map.overlay_global_init(GMarkerGroup.new(true, 1 => marker1, 2 => marker2),"myGroup")
211
+
212
+ Here I have created an active (ie which is going to be displayed when the map is created) marker group called *myGroup* with 2 markers, which I want to reference later. If I did not want to reference them later (I just want to be able to display and undisplay the marker group), I could have passed an array instead of the hash.
213
+
214
+ Then in your template, you could have that:
215
+
216
+ > ... onclick="myGroup.showMarker(1);return false;"
217
+ > ... onclick="myGroup.showMarker(2);return false;"
218
+ > <%= @map.div %>
219
+
220
+ When you click on one of the links, the corresponding marker has its info window displayed.
221
+
222
+ You can call *activate* and *deactivate* to display or undisplay a group of markers. You can add new markers with *addMarker(marker,id)*. Again if you don't care about referencing the marker, you don't need to pass an id. If the marker group is active, the newly added marker will be displayed immediately. Otherwise it will be displayed the next time the group is activated. Finally, since it is an overlay, the group will be removed when calling clearOverlays on the GMap object.
223
+
224
+ You can center and zoom on all the markers in the group by calling *GMarkerGroup#centerAndZoomOnMarkers()* after the group has been added to a map. So for example, if you would want to do that at initalization time, you would do the following (assuming your marker group has been declared as *group*):
225
+ @map.record_init group.center_and_zoom_on_markers
226
+
227
+ ### GMarkerManager
228
+ It is a recent (v2.67) GMaps API class that manages the efficient display of potentially thousands of markers. It is similar to the Clusterer (see below) since markers start appearing at specified zoom levels. The clustering behaviour has to be managed explicitly though by specifying the cluster for smaller zoom levels and specify the expanded cluster for larger zoom levels and so on. Note that it is not an overlay and is not added to the map through an overlay_init call.
229
+
230
+ Here is an example of use:
231
+ > @map = GMap.new("map_div")
232
+ > @map.control_init(:large_map => true, :map_type => true)
233
+ > @map.center_zoom_init([59.91106, 10.72223],3)
234
+ > srand 1234
235
+ > markers1 = []
236
+ > 1.upto(20) do |i|
237
+ > markers1 << GMarker.new([59.91106 + 6 * rand - 3, 10.72223 + 6 * rand - 3],:title => "OY-20-#{i}")
238
+ > end
239
+ > managed_markers1 = ManagedMarker.new(markers1,0,7)
240
+ >
241
+ > markers2 = []
242
+ > 1.upto(200) do |i|
243
+ > markers2 << GMarker.new([59.91106 + 6 * rand - 3, 10.72223 + 6 * rand - 3],:title => "OY-200-#{i}")
244
+ > end
245
+ > managed_markers2 = ManagedMarker.new(markers2,8,9)
246
+ >
247
+ > markers3 = []
248
+ > 1.upto(1000) do |i|
249
+ > markers3 << GMarker.new([59.91106 + 6 * rand - 3, 10.72223 + 6 * rand - 3],:title => "OY-300-#{i}")
250
+ > end
251
+ > managed_markers3= ManagedMarker.new(markers3,10)
252
+ >
253
+ > mm = GMarkerManager.new(@map,:managed_markers => [managed_markers1,managed_markers2,managed_markers3])
254
+ > @map.declare_init(mm,"mgr")
255
+
256
+ ### Clusterer
257
+ A Clusterer is a type of overlay that contains a potentially very large group of markers. It is engineered so markers close to each other and undistinguishable from each other at some level of zoom, appear as only one marker on the map at that level of zoom. Therefore it is possible to have a very large number of markers on the map at the same time and not having the map crawl to a halt in order to display them. It has been slightly modified from Jef Poskanzer's original Clusterer2 code (see http://www.acme.com/javascript/ for the original version). The major difference with that version is that, in YM4R, the clusterer is a GOverlay and can therefore be added to the map with *map.addOverlay(clusterer)* and is cleared along with the rest of overlays when *map.clearOverlays()* is called.
258
+
259
+ **In order to use a clusterer, you should include in your template page the clusterer.js file after the call to *GMap.header* (because it extends the GOverlay class).** You should have something like that in your template:
260
+
261
+ > <%= javascript_include_tag("clusterer") %>
262
+
263
+ To create a clusterer, you should first create an array of all the GMarkers that you want to include in the clusterer (you can still add more after the clusterer has been added to the map though). When GMarkers close together are grouped in one cluster (represented by another marker on the map) and the user clicks on the cluster marker, a list of markers in the cluster is displayed in the info windo with a description: By default it is equal to the +title+ of the markers (which is also displayed as a tooltip when hovering on the marker with the mouse). If you don't want that, you can also pass to the GMarker constructor a key-value pair with key *:description* to have a description different from the title. For example, here is how you would create a clusterer:
264
+
265
+ > markers = [GMarker.new([37.8,-90.4819],:info_window => "Hello",:title => "HOYOYO"),
266
+ > GMarker.new([37.844,-90.47819],:info_window => "Namaste",:description => "Chopoto" , :title => "Ay"),
267
+ > GMarker.new([37.83,-90.456619],:info_window => "Bonjour",:title => "Third"),
268
+ > ]
269
+ > clusterer = Clusterer.new(markers,:max_visible_markers => 2)
270
+
271
+ Of course, with only 3 markers, the whole clusterer thing is totally useless. Usually, using the clusterer starts being interesting with hundreds of markers. The options to pass the clusterer are:
272
+
273
+ * *:max_visible_markers*: Below this number of markers, no clustering is performed. Defaults to 150.
274
+ * *:grid_size*: The clusterer divides the visible area into a grid of this size. Defaults to 5.
275
+ * *:min_markers_per_cluster*: Below this number of markers a cluster is not formed. Defaults to 5.
276
+ * *:max_lines_per_info_box*: Number of lines to display in the info window displayed when a cluster is clicked on by the user. Defaults to 10.
277
+ * *:icon*: Icon to be used to mark a cluster. Defaults to G_DEFAULT_ICON (the classic red marker).
278
+
279
+ Then to add the clusterer to the map at initialization time, you proceed as with a classic overlay:
280
+ > @map.overlay_init clusterer
281
+
282
+ To add new markers in RJS (if the clusterer has been declared globally with overlay_global_init), you should do this:
283
+ > page << raw(clusterer.add_marker(marker,description))
284
+ In this case, the *:description* passed to the GMarker contructor will not be taken into account. Only the *description* passed in the call to +add_marker+ will.
285
+
286
+ ### GeoRss Overlay
287
+ An group of markers taken from a Rss feed containing location information in the W3C basic geo (WGS83 lat/lon) vocabulary and in the Simple GeoRss format. See http://georss.org for more details. The support for GeoRss relies on the MGeoRSS library by Mikel Maron (http://brainoff.com/gmaps/mgeorss.html), although a bit modified, mostly to have the GeoRssOverlay respect the GOverlay API. It has also been enhanced by Andrew Turner who added support for the GeoRss Simple format.
288
+
289
+ **Note that the *geoRssOverlay.js* file must be included in the HTML template in order to use this functionality.** You should have something like that in your template:
290
+ > <%= javascript_include_tag("geoRssOverlay") %>
291
+
292
+ Here is how you would use it. First create the overlay at initialization:
293
+
294
+ > def index
295
+ > @map = GMap.new("map_div")
296
+ > @map.control_init(:large_map => true)
297
+ > @map.center_zoom_init([38.134557,-95.537109],0)
298
+ > @map.overlay_init(GeoRssOverlay.new(url_for(:action => "earthquake_rss"))
299
+ > end
300
+
301
+ Since it is not possible to make requests outside the domain where the current page comes from, there is a need for a proxy. With the GeoRssOverlay initialization above, the request will be made by the *earthquake_rss* action, where the address to find the RSS feed will be hardwired:
302
+
303
+ > def earthquake_rss
304
+ > result = Net::HTTP.get_response("earthquake.usgs.gov","/eqcenter/recenteqsww/catalogs/eqs7day-M5.xml")
305
+ > render :xml => result.body
306
+ > end
307
+
308
+ If you don't want to hardwire the RSS feed location in an action, you can. But you will have to pass the *:proxy* option to the GeoRssOverlay constructor. When requesting the RSS feed, the browser will in fact call the proxy with the actual URL of the RSS feed in the *q* parameter of the request. Here is how you would initialize the GeoRssOverlay that way:
309
+ > @map.overlay_init(GeoRssOverlay.new("http://earthquake.usgs.gov/eqcenter/recenteqsww/catalogs/eqs7day-M5.xml",
310
+ > :proxy => url_for(:action => "proxy")))
311
+ And here is an example of *proxy* action:
312
+ > def proxy
313
+ > result = Net::HTTP.get_response(URI.parse(@params[:q]))
314
+ > render :xml => result.body
315
+ > end
316
+ You should probably do some checks to ensure the proxy is not abused but it is of your responsibility.
317
+
318
+ Another option can be passed to the GeoRssOverlay constructor to set an icon to be used by the markers of the overlay: *:icon*. By default it is equal to the default icon (the classic red one).
319
+
320
+ In the view, you should have something like the following:
321
+ > <html>
322
+ > <head><title>Testing GeoRss</title>
323
+ > <%= raw(GMap.header(:with_vml => false)) %>
324
+ > <%= raw(javascript_include_tag("geoRssOverlay")) %>
325
+ > <%= raw(@map.header_width_height(600,400)) %>
326
+ > <%= raw(@map.to_html) %>
327
+ > </head>
328
+ > <body>
329
+ > <%= raw(@map.div) %>
330
+ > </body>
331
+ > </html>
332
+ Note the inclusion of the *geoRssOverlay.js* file.
333
+
334
+ Other options to pass to the GeoRssOverlay constructor are the following:
335
+
336
+ - *:list_div*: In case you want to make a list of all the markers, with a link on which you can click in order to display the info on the marker, use this option to indicate the ID of the DIV (that you must place yourself on your page).
337
+ - *:list_item_class*: class of the DIV containing each item of the list. Ignored if option *:list_div* is not set.
338
+ - *:limit*: Maximum number of items to display on the map.
339
+ - *:content_div*: Instead of having an info window appear, indicates the ID of the DIV where this info should be displayed.
340
+
341
+ ### Adding new map types
342
+ It is now possible to easily add new map types, on top of the already existing ones, like G_SATELLITE_MAP or G_NORMAL_MAP. The imagery for these new map types can come from layers of the standard map types or can be taken either from a WMS server or from pretiled images on a server (that can be generated with a tool that comes with the YM4R gem: refer to the README of the gem to know more about it).
343
+
344
+ For example, here is how you would setup layers from a public WMS server of the DMAP team of the American Navy:
345
+
346
+ > layer = WMSLayer.new("http://columbo.nrlssc.navy.mil/ogcwms/servlet/WMSServlet/AccuWeather_Maps.wms",
347
+ > "20:3,6:3,0:27,0:29,6:19",
348
+ > :copyright => {'prefix' => "Map Copyright 2006", 'copyright_texts'=> ["DMAP"]},
349
+ > :use_geographic => true, :opacity => 0.8)
350
+
351
+ This sets up a connection to a WMS service, requesting layers *20:3,6:3,0:27,0:29,6:19* (you would have to look at the GetCapabilities document of the service to know what the valid layers are). The copyright notice attributes the data to DMAP. The images will be 80% opaque. For the rest of the options, the default values are used: default styles (*:style* option), PNG format (*:format* option), valid for all zoom levels (*:zoom_range* option). The option *:merc_proj* is irrelevant here since the *:use_geographic* option is true.
352
+
353
+ The arguments *:use_geographic* and *:merc_proj* warrant some explanation. The Google Maps are in the Simple Mercator projection in the WGS84 datum and currently do not support the display of data in projections other than that (at least if you want to display markers and lines on top of it). Unfortunately, different WMS servers do not identify this projection the same way. So you can give to the WMSLayer constructor your server type through the *:merc_proj* option and it will figure out what is the correct identifier. Currently, this works only for *:mapserver* (EPSG:54004) and *:geoserver* (EPSG:41001). For others you can directly pass a number corresponding to the EPSG definition of the simple Mercator projection of your server. On top of that, some servers just don't support the Simple Mercator projection. This is why there is a *:use_geographic* option, that can be set to +true+. It is in order to tell the WMSLayer that it should request its tiles using LatLon coordinates in the WGS84 datum (which should be supported by any server in a consistant way). Unfortunately it is not perfect since the deformation is quite important for low zoom levels (< 5 for the US). Higher than that, the deformation is not that visible. However, if you control the WMS server, it is recommended that you don't use *:use_geographic* and instead use the *:merc_proj* option and setup the Mercator projection in your server if it is not done by default.
354
+
355
+ **Note that you need to include the *wms-gs.js* javascript file in your HTML page in order to make use of the WMSLayer functionality.** You should have something like that in your template:
356
+ <%= javascript_include_tag("wms-gs") %>
357
+ This file uses code by John Deck with the participation of others (read the top of the javascript file to know more).
358
+
359
+ Here is how to define a pretiled layer:
360
+ > layer = PreTiledLayer.new("http://localhost:3000/tiles",
361
+ > :copyright => {'prefix' => "Map C 2006", 'copyright_texts' => ["Open Atlas"]},
362
+ > :zoom_range => 13..13, :opacity => 0.7, :format => "gif")
363
+ I tell the PreTiledLayer constructor where I can find the tiles, setup the Copyright string, the valid zooms for the map, the opacity and the format. Tiles must have standardized names: *tile_#{zoom}_#{x_tile}_#{y_tile}.#{format}* (here the format is "gif"). You can use tools found in the YM4R gem to generate tiles in this format either from local maps or from WMS servers (useful to create tiles from geographic data files without having to run a map server or to cache images from slow servers). Again refer to the documentation of the gem for more information on how to do this.
364
+
365
+ Instead of having the tiles requested directly, you can also decide to have an action on the server which takes care of it. You can used the class PreTiledLayerFromAction for this. In this case, the first argument of the constructor is an url of an action. The arguments *x*, *y* and *z* will be passed to it.
366
+ > layer = PreTiledLayerFromAction.new(url_for(:action => :get_tile),
367
+ > :copyright => {'prefix' => "Map C 2006", 'copyright_texts' => ["Open Atlas"]},
368
+ > :zoom_range => 13..14, :opacity => 0.7)
369
+ The other constructor arguments have the same meaning as PreTiledLayer. Here is an uninteresting example of action that serves tiles:
370
+ > def get_tile
371
+ > x = @params[:x]
372
+ > y = @params[:y]
373
+ > z = @params[:z]
374
+ > begin
375
+ > send_file "#{Rails.root}/public/tiles/tile_#{z}_#{x}_#{y}.png" ,
376
+ > :type => 'image/png', :disposition => 'inline'
377
+ > rescue Exception
378
+ > render :nothing => true
379
+ > end
380
+ > end
381
+
382
+ You can add a layer to a new map type the following way:
383
+ > map_type = GMapType.new(layer,"My WMS")
384
+ This is actually the simplest configuration possible. Your map type has only one data layer and is called "My WMS". You can add more that one layer: Either one that you have created yourself or existing ones. For example:
385
+ > map_type = GMapType.new([GMapType::G_SATELLITE_MAP.get_tile_layers[0],layer,GMapType::G_HYBRID_MAP.get_tile_layers[1]],
386
+ > "Test WMS")
387
+ Here for the "Test WMS" map type, we also take the first layer of the "Satellite" map type in the background and overlay the second layer of the "Hybrid" map type (roads, country boundaries, etc... transparently overlaid on top of the preceding layers) so when the "Test WMS" map type is selected in the interface, all three layers will be displayed.
388
+
389
+ Finally to add a map type to a GMap:
390
+ > @map.add_map_type_init(map_type)
391
+ If you want to wipe out the existing map types (for example the 3 default ones), you can add a *false* argument to the *add_map_type_init* method and the +map_type+ will be the only one.
392
+
393
+ If you want to setup the map as the default one when the map is initially displayed, you should first declare the map type then add it to the map as indicated above and finally setting it as the default map type:
394
+ > @map.declare_init(map_type,"my_map_type")
395
+ > @map.add_map_type_init(map_type)
396
+ > @map.set_map_type_init(map_type)
397
+ Future versions of the plugin may simplify that.
398
+
399
+ ###Google Geocoding
400
+ A helper to perform geocoding on the server side (in Ruby) is included. Here is an example of request:
401
+ > results = Geocoding::get("Rue Clovis Paris")
402
+ You can also pass to the *get* method an options hash to manage the various API key options (see the section on *GMap.header* for details). *results* is an array of Geocoding::Placemark objects, with 2 additional attributes: *status* and *name*. You should check if *status* equals *Geocoding::GEO_SUCCESS* to know if the request has been successful. You can then access the various data elements.
403
+
404
+ Here is an example that will display a marker on Paris:
405
+ > results = Geocoding::get("Rue Clovis Paris")
406
+ > if results.status == Geocoding::GEO_SUCCESS
407
+ > coord = results[0].latlon
408
+ > @map.overlay_init(GMarker.new(coord,:info_window => "Rue Clovis Paris"))
409
+ > end
410
+
411
+ You could also have performed the geocoding on the client side with the following code, which is functionnality equivalent to the code above:
412
+ > GMarker.new("Rue Clovis Paris",:info_window => "Rue Clovis Paris")
413
+
414
+ ### Local Search
415
+ Local Search control has been added to the map and control objects. There are two places you need to implement it to get it to work. This is because the local search control needs an additional library added to the import as well as the control added to the map.
416
+
417
+ In your controller, you add ':local_search => true' to the @map.control_init like this:
418
+ >@map = GMap.new("map_div")
419
+ >@map.control_init(:large_map => true, :map_type => true, :local_search => true)
420
+
421
+ And in your view, you pass ':local_search => true' to the GMap.header like this:
422
+ ><%= GMap.header(:local_search => true) %>
423
+ ><%= @map.to_html %>
424
+ ><%= @map.div(:width => 600, :height => 400) %>
425
+
426
+ You can pass options to the control_init as well. They are:
427
+ >:anchor => [:bottom_left (default), :bottom_right, :top_left, :top_right]
428
+ >:offset_width => 10 (default)
429
+ >:offset_height => 20 (default)
430
+ >:local_search_options => "{suppressZoomToBounds : true,
431
+ > resultList : google.maps.LocalSearch.RESULT_LIST_INLINE,
432
+ > suppressInitialResultSelection : true,
433
+ > searchFormHint : 'Local Search powered by Google',
434
+ > linkTarget : GSearch.LINK_TARGET_BLANK}"
435
+
436
+ So if you wanted the local search control to be at the bottom right of the map, 30 pixel in from the right and 20 pixels above the bottom and some local search options it would look like this:
437
+ >@map.control_init(:large_map => true, :map_type => true, :local_search => true, :anchor => :bottom_right, :offset_width => 30, >:offset_height => 20, :local_search_options => "{suppressZoomToBounds : true, resultList : google.maps.LocalSearch.RESULT_LIST_INLINE, >suppressInitialResultSelection : true, searchFormHint : 'Local Search powered by Google', linkTarget : GSearch.LINK_TARGET_BLANK}")
438
+
439
+ ## Sensor
440
+ Google now requires the 'sensor' attribute in the GET API request. It is defaulted to 'false', but if you are writing an application for one of the new cellphones with GPS out there then you can enable it by using the following call:
441
+
442
+ > <%= GMap.header :sensor => true %>
443
+
444
+ ## Recent changes
445
+ - 'sensor' added to GET URL per Google API Terms of Use. 'false' by default.
446
+ - Local Search overlay added. See above for implementation.
447
+ - GMarker can now be placed by address (in addition to coordinates). Some code to geocode the address when the marker is initialized is added
448
+ - Addition of a +center_zoom_on_points_init+ to center and zoom on a group of pixel
449
+ - In JS, addition of methods to GMap2 and GMarkerGroup to center and zoom on a group of points or markers (thanks to Glen Barnes)
450
+ - Support for easy setup of fullscreen maps
451
+
452
+ ## TODO
453
+ - Add support for easy manipulation of external Google Maps-related libraries: Advanced tooltip manipulation (PdMarker),...
454
+ - Addition of all GeoRss vocabularies (with all features: polylines...) to the geoRssOverlay extension
455
+ - Tutorials
456
+
457
+ ## Disclaimer
458
+ This software is not endorsed in any way by Google.
459
+
460
+ ## Acknowledgement
461
+ The YM4R/GM plugin bundles JavaScript libraries from John Deck (WMS layers on Google Maps), Jef Poskanzer (Clusterer on Google Maps) and Mikel Maron (GeoRss on Google Maps).
462
+
463
+ ## License
464
+ The YM4R/GM plugin is released under the MIT license. The *clusterer.js* file is redistributed with a different license (but still compatible with the MIT license). Check the top of the file in *PLUGIN_ROOT/javascript* to know more.
465
+
466
+ ## Support
467
+ Any questions, enhancement proposals, bug notifications or corrections can be sent to mailto:guilhem.vellut+ym4r@gmail.com.