gmaps4rails 0.4.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc CHANGED
@@ -1,5 +1,6 @@
1
1
  == Google Maps for Rails (gmaps4rails)
2
2
 
3
+
3
4
  Gmaps4rails is developped to simply create a Google Map (Gmaps) from:
4
5
 
5
6
  - model instances (say Users),
@@ -8,7 +9,6 @@ Gmaps4rails is developped to simply create a Google Map (Gmaps) from:
8
9
 
9
10
  It's based on Ruby on Rails 3 Engines and uses Google Maps API V3 with Marker Clusterer in option.
10
11
 
11
-
12
12
  == Installation
13
13
 
14
14
  gem install gmaps4rails
@@ -76,4 +76,6 @@ Done again!
76
76
  Feel free ton contact us, you have your say.
77
77
 
78
78
  == Copyright
79
- MIT license. Authors: Benjamin Roth, David Ruyer
79
+ MIT license.
80
+ Authors: Benjamin Roth, David Ruyer
81
+ Contributor: Alex Vorobiev
@@ -7,22 +7,17 @@
7
7
  <script type="text/javascript" src='http://google-maps-utility-library-v3.googlecode.com/svn/tags/markerclusterer/1.0/src/markerclusterer.js'></script>
8
8
  <%=javascript_include_tag 'gmaps4rails' %>
9
9
  <script type="text/javascript" charset="utf-8">
10
- var styles = [{
11
- url: 'http://google-maps-utility-library-v3.googlecode.com/svn/tags/markerclusterer/1.0/images/people35.png',
12
- height: 35,
13
- width: 35,
14
- opt_anchor: [16, 0],
15
- opt_textColor: '#ff00ff',
16
- opt_textSize: 10
17
- }];
18
10
  <% options.each do |key, value| %>
19
11
  Gmaps4Rails.<%= key %> = <%=raw value.is_a?(String) ? "'#{value}'" : value %>;
20
12
  <% end %>
21
13
  var builder = <%=raw options["processing"] == "json" ? builder : "'#{builder}'" %>;
22
- Gmaps4Rails.initialize(builder);
14
+
15
+ window.onload = function() {
16
+ Gmaps4Rails.initialize(builder);
17
+ }
23
18
  </script>
24
19
  <% end %>
25
20
 
26
- <div id="map-container">
27
- <div id="gmaps4rails_map"></div>
28
- </div>
21
+ <div id="<%= options['map_container_id'] || "map_container" %>">
22
+ <div id="<%= options['map_id'] || "gmaps4rails_map" %>"></div>
23
+ </div>
@@ -10,12 +10,9 @@ module Gmaps4rails
10
10
 
11
11
  def Gmaps4rails.create_json(object)
12
12
  unless object[object.gmaps4rails_options[:lat_column]].blank? && object[object.gmaps4rails_options[:lng_column]].blank?
13
- "{\"description\": \"#{object.gmaps4rails_infowindow}\",
14
- \"longitude\": \"#{object[object.gmaps4rails_options[:lng_column]]}\",
15
- \"latitude\": \"#{object[object.gmaps4rails_options[:lat_column]]}\",
16
- \"picture\": \"#{object.gmaps4rails_marker_picture['picture']}\",
17
- \"width\": \"#{object.gmaps4rails_marker_picture['width']}\",
18
- \"height\": \"#{object.gmaps4rails_marker_picture['height']}\"
13
+ "{
14
+ \"description\": \"#{object.gmaps4rails_infowindow}\", \"title\": \"#{object.gmaps4rails_title}\",
15
+ \"longitude\": \"#{object[object.gmaps4rails_options[:lng_column]]}\", \"latitude\": \"#{object[object.gmaps4rails_options[:lat_column]]}\", \"picture\": \"#{object.gmaps4rails_marker_picture['picture']}\", \"width\": \"#{object.gmaps4rails_marker_picture['width']}\", \"height\": \"#{object.gmaps4rails_marker_picture['height']}\"
19
16
  } ,"
20
17
  end
21
18
  end
@@ -92,6 +89,9 @@ module Gmaps4rails
92
89
  def gmaps4rails_infowindow
93
90
  end
94
91
 
92
+ def gmaps4rails_title
93
+ end
94
+
95
95
  def process_geocoding
96
96
  #to prevent geocoding each time a save is made
97
97
  return true if gmaps4rails_options[:check_process] == true && self[gmaps4rails_options[:checker]] == true
@@ -1,8 +1,20 @@
1
1
  google.load('maps', '3', { other_params: 'sensor=false' });
2
2
 
3
+ //marker_clusterer styles
4
+ var styles = [{
5
+ url: 'http://google-maps-utility-library-v3.googlecode.com/svn/tags/markerclusterer/1.0/images/people35.png',
6
+ height: 35,
7
+ width: 35,
8
+ opt_anchor: [16, 0],
9
+ opt_textColor: '#ff00ff',
10
+ opt_textSize: 10
11
+ }];
12
+
3
13
  var Gmaps4Rails = {
4
14
  processing: 'rails_model',
5
15
  map: null,
16
+ map_id: 'gmaps4rails_map',
17
+ markers: null,
6
18
  marker_picture : "",
7
19
  marker_width : 22,
8
20
  marker_length : 32,
@@ -46,14 +58,40 @@ var Gmaps4Rails = {
46
58
  this.map.fitBounds(this.bounds);
47
59
  }
48
60
  },
61
+
62
+ // clear markers
63
+ clear_markers: function(){
64
+ if (this.markers) {
65
+ for (i in this.markers) {
66
+ this.markers[i].setMap(null);
67
+ }
68
+ this.markers = null;
69
+ }
70
+ },
71
+
72
+ // replace old markers with new markers on an existing map
73
+ replace_markers: function(new_markers){
74
+ this.clear_markers();
75
+ this.locations = [];
76
+ this.add_markers(new_markers);
77
+ },
78
+
79
+ //add new markers to on an existing map (beware, it doesn't check duplicates)
80
+ add_markers: function(new_markers){
81
+ this.locations = this.locations.concat(new_markers);
82
+ this.setup_Markers();
83
+ if (this.auto_adjust) {
84
+ this.map.fitBounds(this.bounds);
85
+ }
86
+ },
49
87
 
50
88
  //resets the map, removes all markers
51
89
  reset_map: function(){
52
- this.map = new google.maps.Map(document.getElementById('gmaps4rails_map'), {
53
- zoom: this.map_zoom,
54
- center: new google.maps.LatLng(this.map_center_latitude, this.map_center_longitude),
55
- mapTypeId: google.maps.MapTypeId.ROADMAP
56
- });
90
+ this.map = new google.maps.Map(document.getElementById(this.map_id), {
91
+ zoom: this.map_zoom,
92
+ center: new google.maps.LatLng(this.map_center_latitude, this.map_center_longitude),
93
+ mapTypeId: google.maps.MapTypeId.ROADMAP
94
+ });
57
95
  },
58
96
 
59
97
  //creates the necessary query to get the model + scope, and sends json to setup_Markers
@@ -76,7 +114,7 @@ var Gmaps4Rails = {
76
114
  //variable used for Auto-adjust
77
115
  this.bounds = new google.maps.LatLngBounds();
78
116
 
79
- //resests Clusterer if needed
117
+ //resets Clusterer if needed
80
118
  if (this.markerClusterer) {
81
119
  this.markerClusterer.clearMarkers();
82
120
  }
@@ -84,17 +122,23 @@ var Gmaps4Rails = {
84
122
  for (var i = 0; i < this.locations.length; ++i) {
85
123
  //test if value passed or use default
86
124
  var marker_picture = this.locations[i].picture != "" && typeof this.locations[i].picture !== "undefined" ? this.locations[i].picture : this.marker_picture;
87
- var marker_width = this.locations[i].width != "" && typeof this.locations[i].width !== "undefined" ? this.locations[i].width : this.marker_width;
88
- var marker_height = this.locations[i].height != "" && typeof this.locations[i].height !== "undefined" ? this.locations[i].height : this.marker_length;
125
+ var marker_width = this.locations[i].width != "" && typeof this.locations[i].width !== "undefined" ? this.locations[i].width : this.marker_width;
126
+ var marker_height = this.locations[i].height != "" && typeof this.locations[i].height !== "undefined" ? this.locations[i].height : this.marker_length;
127
+ var marker_title = this.locations[i].title != "" && typeof this.locations[i].title !== "undefined" ? this.locations[i].title : null;
128
+
89
129
  var myLatLng = new google.maps.LatLng(this.locations[i].latitude, this.locations[i].longitude);
90
-
130
+ //extending bounds, ref: http://unicornless.com/code/google-maps-v3-auto-zoom-and-auto-center
131
+ if (this.auto_adjust) {
132
+ this.bounds.extend(myLatLng);
133
+ }
134
+
91
135
  // Marker sizes are expressed as a Size of X,Y
92
136
  if (marker_picture == "")
93
- { var ThisMarker = new google.maps.Marker({position: myLatLng, map: this.map}); }
137
+ { var ThisMarker = new google.maps.Marker({position: myLatLng, map: this.map, title: marker_title}); }
94
138
  else
95
139
  {
96
140
  var image = new google.maps.MarkerImage(marker_picture, new google.maps.Size(marker_width, marker_height) );
97
- var ThisMarker = new google.maps.Marker({position: myLatLng, map: this.map, icon: image}); //TODO Offer title customization title: "title"
141
+ var ThisMarker = new google.maps.Marker({position: myLatLng, map: this.map, icon: image, title: marker_title});
98
142
  }
99
143
  //save object for later use, basically, to get back the text to display when clicking it
100
144
  this.locations[i].marker_object = ThisMarker;
@@ -102,12 +146,6 @@ var Gmaps4Rails = {
102
146
  markers.push(ThisMarker);
103
147
  //add click listener
104
148
  google.maps.event.addListener(Gmaps4Rails.locations[i].marker_object, 'click', function() { if (Gmaps4Rails.info_window!=null) {Gmaps4Rails.info_window.close();}; Gmaps4Rails.getInfoWindow(this);});
105
-
106
- //extending bounds, ref: http://unicornless.com/code/google-maps-v3-auto-zoom-and-auto-center
107
- if (this.auto_adjust) {
108
- var ll = new google.maps.LatLng(this.locations[i].latitude, this.locations[i].longitude);
109
- this.bounds.extend(ll);
110
- }
111
149
  }
112
150
  this.setup_Clusterer(markers);
113
151
  },
@@ -136,4 +174,4 @@ var Gmaps4Rails = {
136
174
  });
137
175
  }
138
176
  }
139
- };
177
+ };
@@ -18,11 +18,11 @@ describe "Acts as gmappable" do
18
18
 
19
19
  it "should render a valid json from an array of ojects" do
20
20
  @user2 = User.create!(:name => "me", :address => "Paris, France" )
21
- User.all.to_gmaps4rails.should == "[{\"description\": \"\",\n\"longitude\": \"5.9311119\",\n\"latitude\": \"43.1251606\",\n\"picture\": \"\",\n\"width\": \"\",\n\"height\": \"\"\n} ,{\"description\": \"\",\n\"longitude\": \"2.3509871\",\n\"latitude\": \"48.8566667\",\n\"picture\": \"\",\n\"width\": \"\",\n\"height\": \"\"\n} ]"
21
+ User.all.to_gmaps4rails.should == "[{\n\"description\": \"\", \"title\": \"\",\n\"longitude\": \"5.9311119\", \"latitude\": \"43.1251606\", \"picture\": \"\", \"width\": \"\", \"height\": \"\"\n} ,{\n\"description\": \"\", \"title\": \"\",\n\"longitude\": \"2.3509871\", \"latitude\": \"48.8566667\", \"picture\": \"\", \"width\": \"\", \"height\": \"\"\n} ]"
22
22
  end
23
23
 
24
24
  it "should render a valid json from a single object" do
25
- @user.to_gmaps4rails.should == "[{\"description\": \"\",\n\"longitude\": \"5.9311119\",\n\"latitude\": \"43.1251606\",\n\"picture\": \"\",\n\"width\": \"\",\n\"height\": \"\"\n} ]"
25
+ @user.to_gmaps4rails.should == "[{\n\"description\": \"\", \"title\": \"\",\n\"longitude\": \"5.9311119\", \"latitude\": \"43.1251606\", \"picture\": \"\", \"width\": \"\", \"height\": \"\"\n} ]"
26
26
  end
27
27
 
28
28
  it "should not geocode again after address changes if checker is true" do
@@ -52,7 +52,7 @@ describe "Acts as gmappable" do
52
52
  end
53
53
 
54
54
  it "should not set boolean to true when address update fails" do
55
- @user["gmaps"].should_not == true
55
+ @user.gmaps.should_not == true
56
56
  end
57
57
  end
58
58
 
@@ -114,7 +114,7 @@ describe "Acts as gmappable" do
114
114
  @user.long_test.should == 5.9311119
115
115
  @user.longitude.should == nil
116
116
  @user.latitude.should == nil
117
- @user.to_gmaps4rails.should == "[{\"description\": \"\",\n\"longitude\": \"5.9311119\",\n\"latitude\": \"43.1251606\",\n\"picture\": \"\",\n\"width\": \"\",\n\"height\": \"\"\n} ]"
117
+ @user.to_gmaps4rails.should == "[{\n\"description\": \"\", \"title\": \"\",\n\"longitude\": \"5.9311119\", \"latitude\": \"43.1251606\", \"picture\": \"\", \"width\": \"\", \"height\": \"\"\n} ]"
118
118
  end
119
119
 
120
120
  it "should not save the boolean if check_process is false" do
@@ -159,7 +159,7 @@ describe "Acts as gmappable" do
159
159
  end
160
160
  end
161
161
  @user = User.create!(:name => "me", :address => "Toulon, France", :picture => "http://www.blankdots.com/img/github-32x32.png")
162
- @user.to_gmaps4rails.should == "[{\"description\": \"My Beautiful Picture: http://www.blankdots.com/img/github-32x32.png\",\n\"longitude\": \"5.9311119\",\n\"latitude\": \"43.1251606\",\n\"picture\": \"\",\n\"width\": \"\",\n\"height\": \"\"\n} ]"
162
+ @user.to_gmaps4rails.should == "[{\n\"description\": \"My Beautiful Picture: http://www.blankdots.com/img/github-32x32.png\", \"title\": \"\",\n\"longitude\": \"5.9311119\", \"latitude\": \"43.1251606\", \"picture\": \"\", \"width\": \"\", \"height\": \"\"\n} ]"
163
163
  end
164
164
 
165
165
  it "should take into account the picture provided in the model" do
@@ -176,7 +176,17 @@ describe "Acts as gmappable" do
176
176
  end
177
177
  end
178
178
  @user = User.create!(:name => "me", :address => "Toulon, France")
179
- @user.to_gmaps4rails.should == "[{\"description\": \"\",\n\"longitude\": \"5.9311119\",\n\"latitude\": \"43.1251606\",\n\"picture\": \"http://www.blankdots.com/img/github-32x32.png\",\n\"width\": \"32\",\n\"height\": \"32\"\n} ]"
179
+ @user.to_gmaps4rails.should == "[{\n\"description\": \"\", \"title\": \"\",\n\"longitude\": \"5.9311119\", \"latitude\": \"43.1251606\", \"picture\": \"http://www.blankdots.com/img/github-32x32.png\", \"width\": \"32\", \"height\": \"32\"\n} ]"
180
+ end
181
+
182
+ it "should take into account the title provided in the model" do
183
+ User.class_eval do
184
+ def gmaps4rails_title
185
+ "Sweet Title"
186
+ end
187
+ end
188
+ @user = User.create!(:name => "me", :address => "Toulon, France")
189
+ @user.to_gmaps4rails.should == "[{\n\"description\": \"\", \"title\": \"Sweet Title\",\n\"longitude\": \"5.9311119\", \"latitude\": \"43.1251606\", \"picture\": \"http://www.blankdots.com/img/github-32x32.png\", \"width\": \"32\", \"height\": \"32\"\n} ]"
180
190
  end
181
191
  end
182
192
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gmaps4rails
3
3
  version: !ruby/object:Gem::Version
4
- hash: 13
4
+ hash: 11
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 4
9
- - 1
10
- version: 0.4.1
8
+ - 5
9
+ - 0
10
+ version: 0.5.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Benjamin Roth
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-03-07 00:00:00 +01:00
19
+ date: 2011-03-11 00:00:00 +01:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
@@ -58,7 +58,6 @@ files:
58
58
  - lib/tasks/install.rake
59
59
  - public/images/marker.png
60
60
  - public/javascripts/gmaps4rails.js
61
- - public/javascripts/old.js
62
61
  - public/stylesheets/gmaps4rails.css
63
62
  - README.rdoc
64
63
  - test/dummy/app/controllers/application_controller.rb
@@ -1,144 +0,0 @@
1
- google.load('maps', '3', { other_params: 'sensor=false' });
2
-
3
- google.setOnLoadCallback(initialize);
4
- var gmaps4rails_map = null;
5
- var gmaps4rails_data = null;
6
- var markerClusterer = null;
7
- var gmaps4rails_infowindow = null;
8
- var gmaps_circle = null ;
9
-
10
- //put markers on the map + launch the clusterer
11
- function setMarkers(locations) {
12
-
13
- //variable used for Marker Clusterer
14
- var markers = [];
15
-
16
- if (markerClusterer) {
17
- markerClusterer.clearMarkers();
18
- }
19
- // Add markers to the map
20
- for (var i = 0; i < locations.markers.length; ++i) {
21
- // Marker sizes are expressed as a Size of X,Y
22
- var image = new google.maps.MarkerImage(gmaps4rails_marker_picture,
23
- new google.maps.Size(gmaps4rails_marker_width, gmaps4rails_marker_length)
24
- );
25
- var myLatLng = new google.maps.LatLng(locations.markers[i].latitude, locations.markers[i].longitude);
26
- var ThisMarker = new google.maps.Marker({position: myLatLng, map: gmaps4rails_map, icon: image}); //TODO Offer title customization title: "title"
27
- //save object for later use, basically, to get back the text to display when clicking it
28
- locations.markers[i].marker_object = ThisMarker;
29
- //save the marker again in a list for the clusterer
30
- markers.push(ThisMarker);
31
-
32
- //add click listener
33
- google.maps.event.addListener(locations.markers[i].marker_object, 'click', function() { if (gmaps4rails_infowindow!=null) {gmaps4rails_infowindow.close();}; getInfoWindow(this);});
34
-
35
- }
36
-
37
- markerClusterer = new MarkerClusterer(gmaps4rails_map, markers, {
38
- maxZoom: 10,
39
- gridSize: 50,
40
- //styles: styles TODO: offer clusterer customization
41
- });
42
- }
43
-
44
- //get infowindow content when listener calls it
45
- function getInfoWindow(which)
46
- {
47
- for ( var m = 0; m < gmaps4rails_data.markers.length; ++m )
48
- {
49
- var markerInfo = gmaps4rails_data.markers[m].marker_object;
50
- if ( markerInfo == which )
51
- {
52
- gmaps4rails_infowindow = new google.maps.InfoWindow({content: gmaps4rails_data.markers[m].description });
53
- gmaps4rails_infowindow.open( gmaps4rails_map, which );
54
- return;
55
- }
56
- }
57
- }
58
-
59
- //initializes the map
60
- function create_map(filter_value) {
61
- request = gmaps4rails_base_url + '?model=' + gmaps4rails_model;
62
-
63
- // if(gmaps4rails_scope != null)
64
- // request += '&scope=' + gmaps4rails_scope;
65
-
66
- if (!(filter_value == null))
67
- {
68
- split_filter_value = filter_value.split('+');
69
- if (!(split_filter_value[0] == null))
70
- {
71
- request += '&filter=' + split_filter_value[0];
72
- }
73
- if (!(split_filter_value[1] == null))
74
- {
75
- request += '&options=' + split_filter_value[1];
76
- }
77
- }
78
- jQuery.getJSON(request,
79
- function(data){
80
- gmaps4rails_data = data;
81
- setMarkers(gmaps4rails_data);
82
- });
83
- }
84
-
85
- function initialize() {
86
- gmaps4rails_reset();
87
- //infowindow closes when user clicks on the map
88
- google.maps.event.addListener(gmaps4rails_map, 'click', function()
89
- { if (gmaps4rails_infowindow != null) {gmaps4rails_infowindow.close();}
90
- });
91
- create_map();
92
- }
93
-
94
- function gmaps4rails_resfreshmap() {
95
- gmaps4rails_reset();
96
- var index = document.gmaps4rails_form.gmaps4rails_list.selectedIndex;
97
- var filter_value = document.gmaps4rails_form.gmaps4rails_list.options[index].value;
98
- create_map(filter_value);
99
- }
100
-
101
- function gmaps4rails_reset(){
102
- gmaps4rails_map = new google.maps.Map(document.getElementById('gmaps4rails_map'), {
103
- zoom: gmaps4rails_map_zoom,
104
- center: new google.maps.LatLng(gmaps4rails_map_center_lat, gmaps4rails_map_center_long),
105
- mapTypeId: google.maps.MapTypeId.ROADMAP
106
- });
107
- }
108
-
109
- // max_distance in km
110
- function filter_distance() {
111
- var max_distance = parseInt(document.getElementById('gmaps4rails_user_distance').value, 10);
112
- if (!(max_distance>0 || max_distance<0))
113
- {
114
- alert('Please set the max distance');
115
- }
116
- else{
117
- if (gmaps_circle!=null) { gmaps_circle.setMap(null);}
118
- var myCenter = new google.maps.LatLng(gmaps4rails_ref_lat, gmaps4rails_ref_long);
119
- var filtered_markers = {"markers":[]};
120
-
121
-
122
- for (var i = 0; i < gmaps4rails_data.markers.length; ++i) {
123
- if (get_distance(gmaps4rails_ref_long, gmaps4rails_data.markers[i].longitude, gmaps4rails_ref_lat, gmaps4rails_data.markers[i].latitude) < max_distance)
124
- { filtered_markers.markers.push(gmaps4rails_data.markers[i]);}
125
- setMarkers(filtered_markers);
126
- }
127
- //radius is in meters
128
- gmaps_circle = new google.maps.Circle({radius: max_distance*1000, center: myCenter, fillColor:"#00FF00", strokeColor: "#00EE00"});
129
- gmaps_circle.setMap(gmaps4rails_map);
130
- }
131
- }
132
-
133
- function get_distance(long1, long2, lat1, lat2)
134
- {
135
- var theta = long1 - long2;
136
- var dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
137
- dist = Math.acos(dist);
138
- dist = rad2deg(dist);
139
- var km = dist * 60 * 1.853;
140
- return km;
141
- }
142
-
143
- function deg2rad(value) { return value*Math.PI/180;}
144
- function rad2deg(value) { return value*180/Math.PI;}