gmaps4rails 0.9.1 → 0.10.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -10,6 +10,20 @@ It's based on Ruby on Rails 3 Engines and uses Google Maps API V3.
10
10
 
11
11
  See screencasts here: http://www.youtube.com/user/TheApneadiving
12
12
 
13
+ == When Gmaps4rails finally means Global Maps for Rails
14
+
15
+ I now want to extend the gem, so it would support other map providers. I intend to add:
16
+
17
+ * openlayers
18
+
19
+ * mapquest
20
+
21
+ * bing
22
+
23
+ See: https://github.com/apneadiving/Google-Maps-for-Rails/wiki/Map-Apis
24
+
25
+ Any help would be appreciated.
26
+
13
27
 
14
28
  == Requirements
15
29
  - gem 'gmaps4rails' (in your gemfile, then bundle)
@@ -20,18 +34,18 @@ See screencasts here: http://www.youtube.com/user/TheApneadiving
20
34
  == Basic configuration
21
35
  In your model, add:
22
36
 
23
- acts_as_gmappable
24
-
25
- def gmaps4rails_address
26
- #describe how to retrieve the address from your model, if you use directly a db column, you can dry your code, see wiki
27
- "#{self.street}, #{self.city}, #{self.country}"
28
- end
37
+ acts_as_gmappable
38
+
39
+ def gmaps4rails_address
40
+ #describe how to retrieve the address from your model, if you use directly a db column, you can dry your code, see wiki
41
+ "#{self.street}, #{self.city}, #{self.country}"
42
+ end
29
43
 
30
44
  Create a migration and add the following fields to your table (here users):
31
45
 
32
- add_column :users, :latitude, :float #you can change the name, see wiki
33
- add_column :users, :longitude, :float #you can change the name, see wiki
34
- add_column :users, :gmaps, :boolean #not mandatory, see wiki
46
+ add_column :users, :latitude, :float #you can change the name, see wiki
47
+ add_column :users, :longitude, :float #you can change the name, see wiki
48
+ add_column :users, :gmaps, :boolean #not mandatory, see wiki
35
49
 
36
50
  == How to?
37
51
  === QuickStart!
@@ -67,8 +81,6 @@ Done!
67
81
 
68
82
  * Integrate Markers with Label: http://google-maps-utility-library-v3.googlecode.com/svn/tags/markerwithlabel/1.1.2/
69
83
 
70
- * Other options from here: http://code.google.com/p/google-maps-utility-library-v3/wiki/Libraries
71
-
72
84
  Feel free ton contact us, you have your say.
73
85
 
74
86
  == Copyright
@@ -7,21 +7,48 @@ if enable_css == true %>
7
7
 
8
8
  <% content_for :scripts do %>
9
9
  <% if enable_js == true %>
10
- <script type="text/javascript" src='http://maps.google.com/maps/api/js?sensor=false&libraries=geometry'></script>
11
- <script type="text/javascript" src='http://google-maps-utility-library-v3.googlecode.com/svn/tags/infobox/1.1.5/src/infobox.js'></script>
12
- <%=javascript_include_tag 'gmaps4rails' %>
13
- <script type="text/javascript" src='http://google-maps-utility-library-v3.googlecode.com/svn/tags/markerclusterer/1.0/src/markerclusterer_compiled.js'></script>
10
+ <%= javascript_include_tag 'gmaps4rails' %>
11
+
12
+ <% case options['map_options'].try(:[], 'provider') %>
13
+ <% when "openlayers"%>
14
+ <script src="http://www.openlayers.org/api/OpenLayers.js"></script>
15
+ <%= javascript_include_tag 'gmaps4rails.openlayers.js' %>
16
+ <% when "mapquest" %>
17
+ <script src="http://mapquestapi.com/sdk/js/v6.0.0/mqa.toolkit.js?key=<%= options['map_options'].try(:[], 'provider_key') %>"></script>
18
+ <%= javascript_include_tag 'gmaps4rails.mapquest.js' %>
19
+ <% when "bing" %>
20
+ <script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0"></script>
21
+ <%= javascript_include_tag 'gmaps4rails.bing.js' %>
22
+ <% else %>
23
+ <%= javascript_include_tag 'gmaps4rails.googlemaps.js' %>
24
+ <script type="text/javascript" src='http://maps.google.com/maps/api/js?sensor=false&libraries=geometry'></script>
25
+ <script type="text/javascript" src='http://google-maps-utility-library-v3.googlecode.com/svn/tags/infobox/1.1.5/src/infobox.js'></script>
26
+ <script type="text/javascript" src='http://google-maps-utility-library-v3.googlecode.com/svn/tags/markerclusterer/1.0/src/markerclusterer_compiled.js'></script>
27
+ <% end %>
14
28
  <% end %>
15
29
  <script type="text/javascript" charset="utf-8">
16
30
  function gmaps4rails_init() {
17
- <% #true is passed here to trigger map initialization %>
18
- <%=raw options.to_gmaps4rails(true) %>
19
- }
31
+ <% #true is passed here to trigger map initialization %>
32
+ <%=raw options.to_gmaps4rails(true) %>
33
+ };
20
34
 
21
35
  window.onload = gmaps4rails_init;
22
36
 
23
37
  </script>
24
38
  <% end %>
39
+
40
+
41
+ <% case options['map_options'].try(:[], 'provider') %>
42
+ <% when "mapquest" %>
43
+ <div id="mapQuest" style="width:750px; height:475px;">
44
+ <!-- Todo: change this style dynamically -->
45
+ </div>
46
+ <% when "bing" %>
47
+ <div id="<%= options['map_options'].try(:[],'container_id').nil? ? "map_container" : options['map_options']['container_id'] %>" >
48
+ <div id="<%= options['map_options'].try(:[],'id').nil? ? "gmaps4rails_map" : options['map_options']['id'] %>" class="bing_map"></div>
49
+ </div>
50
+ <% else %>
25
51
  <div id="<%= options['map_options'].try(:[],'container_id').nil? ? "map_container" : options['map_options']['container_id'] %>">
26
52
  <div id="<%= options['map_options'].try(:[],'id').nil? ? "gmaps4rails_map" : options['map_options']['id'] %>"></div>
27
- </div>
53
+ </div>
54
+ <% end %>
data/lib/gmaps4rails.rb CHANGED
@@ -1,11 +1,11 @@
1
1
  if defined?(Rails) && Rails::VERSION::MAJOR == 3
2
2
  module Gmaps4rails
3
3
  require 'rails'
4
+ require 'gmaps4rails/base'
5
+ require 'gmaps4rails/acts_as_gmappable'
4
6
  require 'gmaps4rails/extensions/array'
5
7
  require 'gmaps4rails/extensions/hash'
6
- require 'gmaps4rails/acts_as_gmappable'
7
8
  require 'gmaps4rails/helper/gmaps4rails_helper'
8
- require 'gmaps4rails/base'
9
9
 
10
10
  class Engine < Rails::Engine
11
11
  initializer "static assets" do |app|
@@ -32,9 +32,9 @@ module Gmaps4rails
32
32
  end
33
33
  end
34
34
 
35
- def to_gmaps4rails
35
+ def to_gmaps4rails(&block)
36
36
  json = "["
37
- json += Gmaps4rails.create_json(self).to_s.chop.chop #removes the extra comma
37
+ json += Gmaps4rails.create_json(self, &block).to_s.chop.chop #removes the extra comma
38
38
  json += "]"
39
39
  end
40
40
 
@@ -14,14 +14,18 @@ module Gmaps4rails
14
14
  # Creates the json related to one Object (only tried ActiveRecord objects)
15
15
  # This json will contian the marker's attributes of the object
16
16
 
17
- def Gmaps4rails.create_json(object)
17
+ def Gmaps4rails.create_json(object, &block)
18
18
  unless object.send(object.gmaps4rails_options[:lat_column]).blank? && object.send(object.gmaps4rails_options[:lng_column]).blank?
19
- "{#{Gmaps4rails.description(object)}#{Gmaps4rails.title(object)}#{Gmaps4rails.sidebar(object)}\"longitude\": \"#{object.send(object.gmaps4rails_options[:lng_column])}\", \"latitude\": \"#{object.send(object.gmaps4rails_options[:lat_column])}\"#{Gmaps4rails.picture(object)}},\n"
19
+ "{#{Gmaps4rails.description(object)}#{Gmaps4rails.title(object)}#{Gmaps4rails.sidebar(object)}\"lng\": \"#{object.send(object.gmaps4rails_options[:lng_column])}\", \"lat\": \"#{object.send(object.gmaps4rails_options[:lat_column])}\"#{Gmaps4rails.picture(object)}#{Gmaps4rails.block_handling(object, &block)}},\n"
20
20
  end
21
21
  end
22
22
 
23
- # Returns description if gmaps4rails_infowindow is defined in the model
23
+ # execute block if provided so that it's included in the json string
24
+ def Gmaps4rails.block_handling(object, &block)
25
+ ", " + yield(object) if block_given?
26
+ end
24
27
 
28
+ # Returns description if gmaps4rails_infowindow is defined in the model
25
29
  def Gmaps4rails.description(object)
26
30
  return "\"description\": \"#{object.gmaps4rails_infowindow}\", " if object.respond_to?("gmaps4rails_infowindow")
27
31
  end
@@ -37,7 +41,7 @@ module Gmaps4rails
37
41
  def Gmaps4rails.sidebar(object)
38
42
  return "\"sidebar\": \"#{object.gmaps4rails_sidebar}\"," if object.respond_to?("gmaps4rails_sidebar")
39
43
  end
40
-
44
+
41
45
  # Returns picture options if gmaps4rails_marker_picture is defined in the model
42
46
  def Gmaps4rails.picture(object)
43
47
  if object.respond_to?("gmaps4rails_marker_picture")
@@ -2,10 +2,10 @@ class Array
2
2
  #Scopes on models generate Arrays
3
3
  #this method enables short call to the json creation for all elements in the array
4
4
 
5
- def to_gmaps4rails
5
+ def to_gmaps4rails(&block)
6
6
  json = "["
7
7
  each do |object|
8
- json += Gmaps4rails.create_json(object).to_s
8
+ json += Gmaps4rails.create_json(object, &block).to_s
9
9
  end
10
10
  json.chop!.chop! unless json == "["
11
11
  json += "]"
@@ -29,7 +29,7 @@ class Hash
29
29
  if option_k == "bounds" #particular case
30
30
  result << "Gmaps4Rails.map_options.#{option_k} = #{option_v};"
31
31
  else
32
- result << "Gmaps4Rails.map_options.#{option_k} = #{Gmaps4rails.filter option_v};"
32
+ result << "Gmaps4Rails.map_options.#{option_k} = #{Gmaps4rails.filter option_v};"
33
33
  end
34
34
  end
35
35
  end
@@ -39,32 +39,32 @@ class Hash
39
39
  each do |category, content| #loop through options hash
40
40
  case category
41
41
  when "map_options"
42
- #already taken into account above => nothing to do here
43
- when "direction"
44
- result << "Gmaps4Rails.direction_conf.origin = '#{content["data"]["from"]}';"
45
- result << "Gmaps4Rails.direction_conf.destination = '#{content["data"]["to"]}';"
42
+ #already taken into account above => nothing to do here
43
+ when "direction"
44
+ result << "Gmaps4Rails.direction_conf.origin = '#{content["data"]["from"]}';"
45
+ result << "Gmaps4Rails.direction_conf.destination = '#{content["data"]["to"]}';"
46
46
 
47
47
  content["options"] ||= Array.new
48
48
  content["options"].each do |option_k, option_v|
49
49
  if option_k == "waypoints"
50
50
  waypoints = Array.new
51
- option_v.each do |waypoint|
52
- waypoints << { "location" => waypoint, "stopover" => true }.to_json
53
- end
54
- result << "Gmaps4Rails.direction_conf.waypoints = [#{waypoints * (",")}];"
51
+ option_v.each do |waypoint|
52
+ waypoints << { "location" => waypoint, "stopover" => true }.to_json
53
+ end
54
+ result << "Gmaps4Rails.direction_conf.waypoints = [#{waypoints * (",")}];"
55
55
  else #option_k != "waypoint"
56
- result << "Gmaps4Rails.direction_conf.#{option_k} = #{Gmaps4rails.filter option_v};"
56
+ result << "Gmaps4Rails.direction_conf.#{option_k} = #{Gmaps4rails.filter option_v};"
57
57
  end
58
58
  end #end .each
59
- result << "Gmaps4Rails.create_direction();"
60
- else #default behaviour in case condition
61
- result << "Gmaps4Rails.#{category} = #{content["data"]};"
62
- content["options"] ||= Array.new
63
- content["options"].each do |option_k, option_v|
64
- result << "Gmaps4Rails.#{category}_conf.#{option_k} = #{Gmaps4rails.filter option_v};"
65
- end
66
- result << "Gmaps4Rails.create_#{category}();"
67
- end
59
+ result << "Gmaps4Rails.create_direction();"
60
+ else #default behaviour in case condition
61
+ result << "Gmaps4Rails.#{category} = #{content["data"]};"
62
+ content["options"] ||= Array.new
63
+ content["options"].each do |option_k, option_v|
64
+ result << "Gmaps4Rails.#{category}_conf.#{option_k} = #{Gmaps4rails.filter option_v};"
65
+ end
66
+ result << "Gmaps4Rails.create_#{category}();"
67
+ end
68
68
  end
69
69
  result << "Gmaps4Rails.callback();"
70
70
  result * ('
@@ -0,0 +1,195 @@
1
+ Gmaps4Rails.map_options.type = "road"; // aerial, auto, birdseye, collinsBart, mercator, ordnanceSurvey, road
2
+ Gmaps4Rails.markers_conf.infobox = "description"; // description or htmlContent
3
+
4
+ ////////////////////////////////////////////////////
5
+ /////////////// Basic Objects //////////////
6
+ ////////////////////////////////////////////////////
7
+
8
+ Gmaps4Rails.getMapType = function(){
9
+ switch(Gmaps4Rails.map_options.type)
10
+ {
11
+ case "road":
12
+ return Microsoft.Maps.MapTypeId.road;
13
+ case "aerial":
14
+ return Microsoft.Maps.MapTypeId.aerial;
15
+ case "auto":
16
+ return Microsoft.Maps.MapTypeId.auto;
17
+ case "birdseye":
18
+ return Microsoft.Maps.MapTypeId.birdseye;
19
+ case "collinsBart":
20
+ return Microsoft.Maps.MapTypeId.collinsBart;
21
+ case "mercator":
22
+ return Microsoft.Maps.MapTypeId.mercator;
23
+ case "ordnanceSurvey":
24
+ return Microsoft.Maps.MapTypeId.ordnanceSurvey;
25
+ default:
26
+ return Microsoft.Maps.MapTypeId.auto;
27
+ }
28
+ };
29
+
30
+ Gmaps4Rails.createPoint = function(lat, lng){
31
+ return new Microsoft.Maps.Point(lat, lng);
32
+ };
33
+
34
+ Gmaps4Rails.createLatLng = function(lat, lng){
35
+ return new Microsoft.Maps.Location(lat, lng);
36
+ };
37
+
38
+ Gmaps4Rails.createLatLngBounds = function(){
39
+
40
+ };
41
+
42
+ Gmaps4Rails.createMap = function(){
43
+ return new Microsoft.Maps.Map(document.getElementById(Gmaps4Rails.map_options.id), {
44
+ credentials: Gmaps4Rails.map_options.provider_key,
45
+ mapTypeId: Gmaps4Rails.getMapType(),
46
+ center: Gmaps4Rails.createLatLng(Gmaps4Rails.map_options.center_latitude, Gmaps4Rails.map_options.center_longitude),
47
+ zoom: Gmaps4Rails.map_options.zoom
48
+ });
49
+ };
50
+
51
+ Gmaps4Rails.createSize = function(width, height){
52
+ return new google.maps.Size(width, height);
53
+ };
54
+
55
+ ////////////////////////////////////////////////////
56
+ ////////////////////// Markers /////////////////////
57
+ ////////////////////////////////////////////////////
58
+
59
+ Gmaps4Rails.createMarker = function(args){
60
+ var markerLatLng = Gmaps4Rails.createLatLng(args.Lat, args.Lng);
61
+ var marker;
62
+ // Marker sizes are expressed as a Size of X,Y
63
+ if (args.marker_picture === "" ) {
64
+ marker = new Microsoft.Maps.Pushpin(Gmaps4Rails.createLatLng(args.Lat, args.Lng), {
65
+ draggable: args.marker_draggable,
66
+ anchor: Gmaps4Rails.createImageAnchorPosition(args.Lat, args.Lng),
67
+ text: args.marker_title
68
+ }
69
+ );
70
+ } else {
71
+ marker = new Microsoft.Maps.Pushpin(Gmaps4Rails.createLatLng(args.Lat, args.Lng), {
72
+ draggable: args.marker_draggable,
73
+ anchor: Gmaps4Rails.createImageAnchorPosition(args.Lat, args.Lng),
74
+ icon: args.marker_picture,
75
+ height: args.marker_height,
76
+ text: args.marker_title,
77
+ width: args.marker_width
78
+ }
79
+ );
80
+ }
81
+ Gmaps4Rails.addToMap(marker);
82
+ return marker;
83
+ };
84
+
85
+ // clear markers
86
+ Gmaps4Rails.clearMarkers = function() {
87
+ for (var i = 0; i < this.markers.length; ++i) {
88
+ this.clearMarker(this.markers[i]);
89
+ }
90
+ };
91
+
92
+ Gmaps4Rails.clearMarker = function(marker) {
93
+ this.removeFromMap(marker.serviceObject);
94
+ };
95
+
96
+ //show and hide markers
97
+ Gmaps4Rails.showMarkers = function() {
98
+ for (var i = 0; i < this.markers.length; ++i) {
99
+ this.showMarker(this.markers[i]);
100
+ }
101
+ };
102
+
103
+ Gmaps4Rails.showMarker = function(marker) {
104
+ marker.serviceObject.setOptions({ visible: true });
105
+ };
106
+
107
+ Gmaps4Rails.hideMarkers = function() {
108
+ for (var i = 0; i < this.markers.length; ++i) {
109
+ this.hideMarker(this.markers[i]);
110
+ }
111
+ };
112
+
113
+ Gmaps4Rails.hideMarker = function(marker) {
114
+ marker.serviceObject.setOptions({ visible: false });
115
+ };
116
+
117
+ Gmaps4Rails.extendBoundsWithMarkers = function(){
118
+ var locationsArray = [];
119
+ for (var i = 0; i < this.markers.length; ++i) {
120
+ locationsArray.push(Gmaps4Rails.markers[i].serviceObject.getLocation());
121
+ }
122
+ Gmaps4Rails.boundsObject = Microsoft.Maps.LocationRect.fromLocations(locationsArray);
123
+ };
124
+
125
+
126
+ ////////////////////////////////////////////////////
127
+ /////////////////// Clusterer //////////////////////
128
+ ////////////////////////////////////////////////////
129
+
130
+ Gmaps4Rails.createClusterer = function(markers_array){
131
+
132
+ };
133
+
134
+ Gmaps4Rails.clearClusterer = function() {
135
+ };
136
+
137
+ //creates clusters
138
+ Gmaps4Rails.clusterize = function()
139
+ {
140
+
141
+ };
142
+
143
+ ////////////////////////////////////////////////////
144
+ /////////////////// INFO WINDOW ////////////////////
145
+ ////////////////////////////////////////////////////
146
+
147
+ // creates infowindows
148
+ Gmaps4Rails.createInfoWindow = function(marker_container){
149
+ var info_window;
150
+ if (Gmaps4Rails.exists(marker_container.description)) {
151
+ //create the infowindow
152
+ if (Gmaps4Rails.markers_conf.infobox == "description") {
153
+ marker_container.info_window = new Microsoft.Maps.Infobox(marker_container.serviceObject.getLocation(), { description: marker_container.description, visible: false, showCloseButton: true});
154
+ }
155
+ else {
156
+ marker_container.info_window = new Microsoft.Maps.Infobox(marker_container.serviceObject.getLocation(), { htmlContent: marker_container.description, visible: false});
157
+ }
158
+
159
+ //add the listener associated
160
+ Microsoft.Maps.Events.addHandler(marker_container.serviceObject, 'click', Gmaps4Rails.openInfoWindow(marker_container.info_window));
161
+ Gmaps4Rails.addToMap(marker_container.info_window);
162
+ }
163
+ };
164
+
165
+ Gmaps4Rails.openInfoWindow = function(infoWindow) {
166
+ return function() {
167
+ // Close the latest selected marker before opening the current one.
168
+ if (Gmaps4Rails.visibleInfoWindow) {
169
+ Gmaps4Rails.visibleInfoWindow.setOptions({ visible: false });
170
+ }
171
+ infoWindow.setOptions({ visible:true });
172
+ Gmaps4Rails.visibleInfoWindow = infoWindow;
173
+ };
174
+ };
175
+
176
+ ////////////////////////////////////////////////////
177
+ /////////////////// Other methods //////////////////
178
+ ////////////////////////////////////////////////////
179
+
180
+ Gmaps4Rails.fitBounds = function(){
181
+ Gmaps4Rails.map.setView({bounds: Gmaps4Rails.boundsObject});
182
+ };
183
+
184
+ Gmaps4Rails.addToMap = function(object){
185
+ Gmaps4Rails.map.entities.push(object);
186
+ };
187
+
188
+ Gmaps4Rails.removeFromMap = function(object){
189
+ console.log(object);
190
+ Gmaps4Rails.map.entities.remove(object);
191
+ };
192
+
193
+ Gmaps4Rails.centerMapOnUser = function(){
194
+ Gmaps4Rails.map.setView({ center: Gmaps4Rails.userLocation});
195
+ };