gmaps4rails 0.7.4 → 0.7.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -21,7 +21,7 @@ module Gmaps4rails
21
21
  end
22
22
  end
23
23
 
24
- def Gmaps4rails.geocode(address)
24
+ def Gmaps4rails.geocode(address, raw = false)
25
25
  if address.nil? || address.empty?
26
26
  raise Gmaps4rails::GeocodeInvalidQuery, "You must provide an address"
27
27
  else #coordinates are valid
@@ -38,12 +38,14 @@ module Gmaps4rails
38
38
  #logger.debug "Google geocoding. Address: #{address}. Result: #{resp.body}"
39
39
  #check if google went well
40
40
  if parse["status"] == "OK"
41
+ return parse if raw == true
41
42
  array = []
42
43
  parse["results"].each do |result|
43
44
  array << {
44
45
  :lat => result["geometry"]["location"]["lat"],
45
46
  :lng => result["geometry"]["location"]["lng"],
46
- :matched_address => result["formatted_address"]
47
+ :matched_address => result["formatted_address"],
48
+ :bounds => result["geometry"]["bounds"]
47
49
  }
48
50
  end
49
51
  return array
data/lib/hash.rb CHANGED
@@ -22,7 +22,11 @@ class Hash
22
22
  #because map should be initialized first, we must extract possible map_options
23
23
  unless self["map_options"].nil?
24
24
  self["map_options"].each do |option_k, option_v|
25
+ if option_k == "bounds" #particular case
26
+ result << "Gmaps4Rails.map_options.#{option_k} = #{option_v};"
27
+ else
25
28
  result << "Gmaps4Rails.map_options.#{option_k} = #{Gmaps4rails.filter option_v};"
29
+ end
26
30
  end
27
31
  end
28
32
 
@@ -14,8 +14,9 @@ var Gmaps4Rails = {
14
14
  zoom : 1,
15
15
  maxZoom: null,
16
16
  minZoom: null,
17
- auto_adjust : false, //adjust the map to the markers if set to true
18
- auto_zoom: true //zoom given by auto-adjust
17
+ auto_adjust : false, //adjust the map to the markers if set to true
18
+ auto_zoom: true, //zoom given by auto-adjust
19
+ bounds: [] //adjust map to these limits only (not taken into account if auto_adjust == true). Should be filled with SW and NE
19
20
  },
20
21
 
21
22
  //markers + info styling
@@ -33,15 +34,11 @@ var Gmaps4Rails = {
33
34
  },
34
35
 
35
36
  //Stored variables
36
- markers : [], //contains all markers, each marker contain a google Marker object in google_object
37
- bounds: null, //contains current bounds
38
- polygons: null, //contains raw data, array of arrays (first element cold be a hash containing options)
39
- polygon_objects: [], //contains processed google.maps.Polygon
40
- polylines: null, //contains raw data, array of arrays (first element cold be a hash containing options)
41
- polyline_objects: [], //contains processed google.maps.Polyline
42
- circles: null, //contains raw data, array of hash
43
- circle_objects: [], //contains processed google.maps.Circle
44
- info_window : null,
37
+ markers : [], //contains all markers. A marker contains the following: {"description": , "longitude": , "title":, "latitude":, "picture": "", "width": "", "length": "", "sidebar": "", "google_object": google_marker}
38
+ bounds_object: null, //contains current bounds from markers, polylines etc...
39
+ polygons: [], //contains raw data, array of arrays (first element could be a hash containing options)
40
+ polylines: [], //contains raw data, array of arrays (first element could be a hash containing options)
41
+ circles: [], //contains raw data, array of hash
45
42
  markerClusterer: null, //contains all marker clusterers
46
43
 
47
44
  //Polygon Styling
@@ -92,15 +89,11 @@ var Gmaps4Rails = {
92
89
  center: new google.maps.LatLng(this.map_options.center_latitude, this.map_options.center_longitude),
93
90
  mapTypeId: google.maps.MapTypeId[this.map_options.type]
94
91
  });
95
- //infowindow closes when user clicks on the map
96
- google.maps.event.addListener(this.map, 'click', function()
97
- { if (this.info_window != null) {this.info_window.close();}
98
- });
99
- //variable used for Auto-adjust
100
- this.bounds = new google.maps.LatLngBounds();
101
-
92
+ //resets sidebar if needed
93
+ this.reset_sidebar_content();
94
+ //launch callbacks if any
102
95
  if(typeof gmaps4rails_callback == 'function') {
103
- gmaps4rails_callback();
96
+ gmaps4rails_callback();
104
97
  }
105
98
  },
106
99
 
@@ -150,7 +143,6 @@ var Gmaps4Rails = {
150
143
  if (this.exists(this.circles[i].latitude) && this.exists(this.circles[i].longitude))
151
144
  {
152
145
  center = new google.maps.LatLng(this.circles[i].latitude, this.circles[i].longitude);
153
- this.extend_bounds(center);
154
146
  //always check if a config is given, if not, use defaults
155
147
  var circle = new google.maps.Circle({
156
148
  center: center,
@@ -162,7 +154,7 @@ var Gmaps4Rails = {
162
154
  radius: this.circles[i].radius,
163
155
  clickable: false
164
156
  });
165
- this.circle_objects.push(circle);
157
+ this.circles[i].google_object = circle;
166
158
  circle.setMap(this.map);
167
159
  }
168
160
  }
@@ -171,23 +163,7 @@ var Gmaps4Rails = {
171
163
  //polygons is an array of arrays. It loops.
172
164
  create_polygons: function(){
173
165
  for (var i = 0; i < this.polygons.length; ++i) {
174
- //Polygons could be customized. By convention, customization options should be contained in the first
175
- if (i==0)
176
- {
177
- //Array contain polygon elements
178
- if (this.polygons[i] instanceof Array) {
179
- this.create_polygon(i);
180
- }
181
- //hashes contain configuration which would be set as default
182
- else{
183
- if (this.exists(this.polygons[i].strokeColor) ) { this.polygons_conf.strokeColor = this.polygons[i].strokeColor; }
184
- if (this.exists(this.polygons[i].strokeOpacity)) { this.polygons_conf.strokeOpacity = this.polygons[i].strokeOpacity; }
185
- if (this.exists(this.polygons[i].strokeWeight )) { this.polygons_conf.strokeWeight = this.polygons[i].strokeWeight; }
186
- if (this.exists(this.polygons[i].fillColor )) { this.polygons_conf.fillColor = this.polygons[i].fillColor; }
187
- if (this.exists(this.polygons[i].fillOpacity )) { this.polygons_conf.fillOpacity = this.polygons[i].fillOpacity; }
188
- }
189
- }
190
- else { this.create_polygon(i); }
166
+ this.create_polygon(i)
191
167
  }
192
168
  },
193
169
 
@@ -203,7 +179,6 @@ var Gmaps4Rails = {
203
179
  for (var j = 0; j < this.polygons[i].length; ++j) {
204
180
  var latlng = new google.maps.LatLng(this.polygons[i][j].latitude, this.polygons[i][j].longitude);
205
181
  polygon_coordinates.push(latlng);
206
- this.extend_bounds(latlng);
207
182
  //first element of an Array could contain specific configuration for this particular polygon. If no config given, use default
208
183
  if (j==0) {
209
184
  strokeColor = this.polygons[i][j].strokeColor || this.polygons_conf.strokeColor;
@@ -225,28 +200,14 @@ var Gmaps4Rails = {
225
200
  clickable: false
226
201
  });
227
202
  //save polygon in list
228
- this.polygon_objects.push(new_poly);
203
+ this.polygons[i].google_object = new_poly;
229
204
  new_poly.setMap(this.map);
230
205
  },
231
206
 
232
207
  //polylines is an array of arrays. It loops.
233
208
  create_polylines: function(){
234
209
  for (var i = 0; i < this.polylines.length; ++i) {
235
- //Polylines could be customized. By convention, customization options should be contained in the first
236
- if (i==0)
237
- {
238
- //Array contain polyline elements
239
- if (this.polylines[i] instanceof Array) {
240
- this.create_polyline(i);
241
- }
242
- //hashes contain configuration which would be set as default
243
- else{
244
- if (this.exists(this.polylines[i].strokeColor) ) { this.polylines_conf.line_strokeColor = this.polygons[i].strokeColor; }
245
- if (this.exists(this.polylines[i].strokeOpacity) ) { this.polylines_conf.line_strokeOpacity = this.polygons[i].strokeOpacity;}
246
- if (this.exists(this.polylines[i].strokeWeight) ) { this.polylines_conf.line_strokeWeight = this.polygons[i].strokeWeight; }
247
- }
248
- }
249
- else { this.create_polyline(i); }
210
+ this.create_polyline(i);
250
211
  }
251
212
  },
252
213
 
@@ -265,7 +226,6 @@ var Gmaps4Rails = {
265
226
  //loop through every point in the array
266
227
  for (var k = 0; k < decoded_array.length; ++k) {
267
228
  polyline_coordinates.push(decoded_array[k]);
268
- this.extend_bounds(decoded_array[k]);
269
229
  polyline_coordinates.push(decoded_array[k]);
270
230
  }
271
231
  }
@@ -282,7 +242,6 @@ var Gmaps4Rails = {
282
242
  {
283
243
  var latlng = new google.maps.LatLng(this.polylines[i][j].latitude, this.polylines[i][j].longitude);
284
244
  polyline_coordinates.push(latlng);
285
- this.extend_bounds(latlng);
286
245
  }
287
246
  }
288
247
  }
@@ -295,17 +254,9 @@ var Gmaps4Rails = {
295
254
  clickable: false
296
255
  });
297
256
  //save polyline
298
- this.polyline_objects.push(new_poly);
257
+ this.polylines[i].google_object = new_poly;
299
258
  new_poly.setMap(this.map);
300
259
  },
301
-
302
- //Two options:
303
- // 1- processing == "rails_model" && builder = model_name
304
- // 2- processing == "json" && builder = json in format: [{"description": , "longitude": , "title":, "latitude":, "picture": "", "width": "", "length": ""}]
305
- create_markers: function() {
306
- this.setup_Markers();
307
- this.adjust();
308
- },
309
260
 
310
261
  // clear markers
311
262
  clear_markers: function(){
@@ -321,16 +272,20 @@ var Gmaps4Rails = {
321
272
  replace_markers: function(new_markers){
322
273
  //reset previous markers
323
274
  this.markers = new Array;
324
- //reset Auto-adjust
325
- this.bounds = new google.maps.LatLngBounds();
275
+ //reset current bounds
276
+ this.google_bounds = new google.maps.LatLngBounds();
326
277
  //reset sidebar content if exists
278
+ this.reset_sidebar_content();
279
+ //add new markers
280
+ this.add_markers(new_markers);
281
+ },
282
+
283
+ reset_sidebar_content: function(){
327
284
  if (this.markers_conf.list_container != null ){
328
285
  var ul = document.getElementById(this.markers_conf.list_container);
329
286
  ul.innerHTML = "";
330
287
  }
331
- //add new markers
332
- this.add_markers(new_markers);
333
- },
288
+ },
334
289
 
335
290
  //add new markers to on an existing map (beware, it doesn't check duplicates)
336
291
  add_markers: function(new_markers){
@@ -339,15 +294,19 @@ var Gmaps4Rails = {
339
294
  //update the list of markers to take into account
340
295
  this.markers = this.markers.concat(new_markers);
341
296
  //put markers on the map
342
- this.setup_Markers();
343
- //adjust map
344
- this.adjust();
297
+ this.create_markers();
345
298
  },
346
299
 
347
- //Creates Marker from the markers passed + markerClusterer
348
- setup_Markers: function () {
349
- // Add markers to the map
350
- for (var i = 0; i < this.markers.length; ++i) {
300
+ //creates, clusterizes and adjusts map
301
+ create_markers: function() {
302
+ this.create_google_markers_from_markers();
303
+ this.clusterize();
304
+ this.adjust_map_to_bounds();
305
+ },
306
+
307
+ //create google.maps Markers from data provided by user
308
+ create_google_markers_from_markers: function(){
309
+ for (var i = 0; i < this.markers.length; ++i) {
351
310
  //check if the marker has not already been created
352
311
  if (!this.exists(this.markers[i].google_object)) {
353
312
  //test if value passed or use default
@@ -367,7 +326,6 @@ var Gmaps4Rails = {
367
326
  }
368
327
 
369
328
  var myLatLng = new google.maps.LatLng(Lat, Lng);
370
- this.extend_bounds(myLatLng);
371
329
 
372
330
  // Marker sizes are expressed as a Size of X,Y
373
331
  if (marker_picture == "")
@@ -379,18 +337,26 @@ var Gmaps4Rails = {
379
337
  }
380
338
  //save object
381
339
  this.markers[i].google_object = ThisMarker;
382
- //add infowindowstuff + list creation if enabled
383
- this.handle_info_window(this.markers[i]);
340
+ //add infowindowstuff if enabled
341
+ this.create_info_window(this.markers[i]);
342
+ //create sidebar if enabled
343
+ this.create_sidebar(this.markers[i]);
384
344
  }
385
345
  }
386
- this.setup_Clusterer();
346
+
387
347
  },
388
348
 
389
- handle_info_window: function(marker_container){
349
+ // creates infowindows
350
+ create_info_window: function(marker_container){
390
351
  //create the infowindow
391
352
  var info_window = new google.maps.InfoWindow({content: marker_container.description });
392
353
  //add the listener associated
393
354
  google.maps.event.addListener(marker_container.google_object, 'click', this.openInfoWindow(info_window, marker_container.google_object));
355
+
356
+ },
357
+
358
+ //creates sidebar
359
+ create_sidebar: function(marker_container){
394
360
  if (this.markers_conf.list_container)
395
361
  {
396
362
  var ul = document.getElementById(this.markers_conf.list_container);
@@ -399,11 +365,19 @@ var Gmaps4Rails = {
399
365
  aSel.href = 'javascript:void(0);';
400
366
  var html = this.exists(marker_container.sidebar) ? marker_container.sidebar : "Marker";
401
367
  aSel.innerHTML = html;
402
- aSel.onclick = this.generateTriggerCallback(marker_container.google_object, 'click');
368
+ aSel.onclick = this.sidebar_element_handler(marker_container.google_object, 'click');
403
369
  li.appendChild(aSel);
404
370
  ul.appendChild(li);
405
371
  }
406
372
  },
373
+
374
+ //moves map to marker clicked + open infowindow
375
+ sidebar_element_handler: function(marker, eventType) {
376
+ return function() {
377
+ Gmaps4Rails.map.panTo(marker.position);
378
+ google.maps.event.trigger(marker, eventType);
379
+ };
380
+ },
407
381
 
408
382
  openInfoWindow: function(infoWindow, marker) {
409
383
  return function() {
@@ -417,14 +391,8 @@ var Gmaps4Rails = {
417
391
  };
418
392
  },
419
393
 
420
- generateTriggerCallback: function(marker, eventType) {
421
- return function() {
422
- Gmaps4Rails.map.panTo(marker.position);
423
- google.maps.event.trigger(marker, eventType);
424
- };
425
- },
426
-
427
- setup_Clusterer: function()
394
+ //creates clusters
395
+ clusterize: function()
428
396
  {
429
397
  if (this.markers_conf.do_clustering == true)
430
398
  {
@@ -442,11 +410,55 @@ var Gmaps4Rails = {
442
410
  },
443
411
 
444
412
  //to make the map fit the different LatLng points
445
- extend_bounds: function(latlng) {
446
- //extending bounds, ref: http://unicornless.com/code/google-maps-v3-auto-zoom-and-auto-center
447
- if (this.map_options.auto_adjust) {
448
- this.bounds.extend(latlng);
449
- }
413
+ adjust_map_to_bounds: function(latlng) {
414
+
415
+ //FIRST_STEP: retrieve all bounds
416
+ //create the bounds object only if necessary
417
+ if (this.map_options.auto_adjust || this.map_options.bounds != null) {
418
+ this.google_bounds = new google.maps.LatLngBounds();
419
+ }
420
+
421
+ //if autodjust is true, must get bounds from markers polylines etc...
422
+ if (this.map_options.auto_adjust) {
423
+ //from markers
424
+ for (var i = 0; i < this.markers.length; ++i) {
425
+ this.google_bounds.extend(this.markers[i].google_object.position);
426
+ }
427
+ //from polygons:
428
+ for (var i = 0; i < this.polylines.length; ++i) {
429
+ this.polylines[i].google_object.latLngs.forEach(function(obj1){ obj1.forEach(function(obj2){ Gmaps4Rails.google_bounds.extend(obj2);} );})
430
+ }
431
+ //from polylines:
432
+ for (var i = 0; i < this.polygons.length; ++i) {
433
+ this.polygons[i].google_object.latLngs.forEach(function(obj1){ obj1.forEach(function(obj2){ Gmaps4Rails.google_bounds.extend(obj2);} );})
434
+ }
435
+ //from circles
436
+ for (var i = 0; i < this.circles.length; ++i) {
437
+ this.google_bounds.extend(this.circles[i].google_object.getBounds().getNorthEast());
438
+ this.google_bounds.extend(this.circles[i].google_object.getBounds().getSouthWest());
439
+ }
440
+ }
441
+ //in every case, I've to take into account the bounds set up by the user
442
+ for (var i = 0; i < this.map_options.bounds.length; ++i) {
443
+ //create points from bounds provided
444
+ var bound = new google.maps.LatLng(this.map_options.bounds[i].lat, this.map_options.bounds[i].lng);
445
+ this.google_bounds.extend(bound);
446
+ }
447
+
448
+ //SECOND_STEP: ajust the map to the bounds
449
+ if (this.map_options.auto_adjust || this.map_options.bounds.length > 0) {
450
+
451
+ //if autozoom is false, take user info into account
452
+ if(!this.map_options.auto_zoom) {
453
+ var map_center = this.google_bounds.getCenter();
454
+ this.map_options.center_longitude = map_center.lat();
455
+ this.map_options.center_latitude = map_center.lng();
456
+ this.map.setCenter(map_center);
457
+ }
458
+ else {
459
+ this.map.fitBounds(this.google_bounds);
460
+ }
461
+ }
450
462
  },
451
463
 
452
464
  //basic function to check existence of a variable
@@ -464,32 +476,7 @@ var Gmaps4Rails = {
464
476
  return [Lat, Lng];
465
477
  },
466
478
 
467
- //adjust or not center of the map. Takes into account auto_adjust & auto_adjust
468
- adjust: function(){
469
- if (this.map_options.auto_adjust) {
470
- if(this.map_options.auto_zoom) {
471
- this.map.fitBounds(this.bounds);
472
- }
473
- else {
474
- var map_center = this.bounds.getCenter();
475
- this.map_options.center_longitude = map_center.Da;
476
- this.map_options.center_latitude = map_center.Ba;
477
- this.map.setCenter(map_center);
478
- }
479
- }
480
- },
481
-
482
- //retrives a value between -1 and 1
479
+ //gives a value between -1 and 1
483
480
  random: function() { return ( Math.random() * 2 -1); }
484
481
 
485
- };
486
-
487
- //marker_clusterer styles
488
- // var styles = [{
489
- // url: 'http://google-maps-utility-library-v3.googlecode.com/svn/tags/markerclusterer/1.0/images/people35.png',
490
- // height: 35,
491
- // width: 35,
492
- // opt_anchor: [16, 0],
493
- // opt_textColor: '#ff00ff',
494
- // opt_textSize: 10
495
- // }];
482
+ };
@@ -1,6 +1,6 @@
1
1
  class User < ActiveRecord::Base
2
2
  acts_as_gmappable
3
-
3
+
4
4
  def gmaps4rails_address
5
5
  address
6
6
  end
@@ -10,6 +10,7 @@ class User < ActiveRecord::Base
10
10
  # end
11
11
  #
12
12
  def gmaps4rails_infowindow
13
- "je suis l'infowindow"
13
+ "je suis l'infowindow de #{name}"
14
14
  end
15
+
15
16
  end
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  describe "Geocode" do
4
4
 
5
5
  it "should geocode properly an address" do
6
- Gmaps4rails.geocode("alaska").should == [{:lat=>63.588753, :lng=>-154.4930619, :matched_address=>"Alaska, USA"}]
6
+ Gmaps4rails.geocode("alaska").should == [{:matched_address=>"Alaska, USA", :bounds=>{"northeast"=>{"lng"=>-129.979511, "lat"=>71.441059}, "southwest"=>{"lng"=>172.347846, "lat"=>51.175092}}, :lat=>63.588753, :lng=>-154.4930619}]
7
7
  end
8
8
 
9
9
  it "should raise an error when address invalid" do
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: 11
4
+ hash: 9
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 7
9
- - 4
10
- version: 0.7.4
9
+ - 5
10
+ version: 0.7.5
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-04-08 00:00:00 +02:00
19
+ date: 2011-04-09 00:00:00 +02:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency