2009年3月24日 星期二

YM4R/GM plugin for Rails

Binding JavaScript and Ruby
At initialization time, you can pass arbitrary JavaScript code to the GMap#record_init and GMap#record_global_init.

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.

Initialization of the map

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.

The other absolutely necessary initialization step in the controller is the setting of center and zoom:



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

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 key

you can override all the key settings in the configuration by passing a value to the :key key.



GMap#to_html
By default, it outputs the script tags and initializes the map in response to the onload event of the JavaScript window object

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.

So you should have something like the following:

Note that you need to set a size for the map DIV element at some point or the map will not be displayed.




GMarkers
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).





Update of the map

沒有留言: