rails-google-maps 0.0.14 → 0.0.15

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -34,6 +34,12 @@ And include it in your ```application.js``` file after jquery:
34
34
 
35
35
  ## Usage
36
36
  ####SimpleForm usage:
37
+ Google maps autocomplete.
38
+ in ```application.js```:
39
+ ```
40
+ //= require rails-google-maps/autocomplete
41
+ ```
42
+ in template:
37
43
  ```erb
38
44
  /*NOTE: gmap-canvas - default value and can be ommited*/
39
45
  <%= f.input :address, as: :google_maps_autocomplete, input_html: { gmap_id: 'gmap_canvas' } %>
@@ -41,6 +47,8 @@ And include it in your ```application.js``` file after jquery:
41
47
  <div id="gmap_canvas"></div>
42
48
  ```
43
49
  Additionally on edit address point will be marked on the map.
50
+
51
+ That is all, if you want to use it without SimpleForm read next part.
44
52
  ####Javascript usage:
45
53
  1) Apply google map to div element where map should appear:
46
54
  ```js
@@ -60,9 +68,9 @@ new GoogleMap({longitudeInput: '#longitude_input', latitudeInput: '#latitude_inp
60
68
  ```js
61
69
  gmap = new GoogleMap()
62
70
  gmap.apply()
63
- gmap.geocodeLookup('address', 'New York')
71
+ gmap.setMarker('address', 'New York')
64
72
  //or
65
- gmap.geocodeLookup('latLng', new google.maps.LatLng(51.751724,-1.255284))
73
+ gmap.setMarker('latLng', new google.maps.LatLng(51.751724,-1.255284))
66
74
  ```
67
75
 
68
76
  4) Prevent users from changing location:
@@ -2,13 +2,16 @@ class GoogleMapsAutocompleteInput < SimpleForm::Inputs::StringInput
2
2
  include ActionView::Helpers::TagHelper
3
3
  def input
4
4
  gmap_id = input_html_options.delete(:gmap_id)
5
+ apply = input_html_options.key?(:apply) ? input_html_options.delete(:apply) : true
6
+ lat_id = input_html_options.key?(:lat_id) ? input_html_options.delete(:lat_id) : nil
7
+ lng_id = input_html_options.key?(:lng_id) ? input_html_options.delete(:lng_id) : nil
5
8
  input = @builder.text_field(attribute_name, input_html_options)
6
- (input + content_tag('script', script_text(gmap_id, input).html_safe)).html_safe
9
+ (input + content_tag('script', script_text(gmap_id, input, apply, lat_id, lng_id).html_safe)).html_safe
7
10
  end
8
11
 
9
12
  private
10
13
 
11
- def script_text(gmap_id, tag)
14
+ def script_text(gmap_id, tag, apply, lat_id, lng_id)
12
15
  "$(document).ready(function(){
13
16
  if(typeof google == 'undefined')
14
17
  return;
@@ -20,11 +23,14 @@ class GoogleMapsAutocompleteInput < SimpleForm::Inputs::StringInput
20
23
  input = $('#{tag}');
21
24
  var id = input.attr('id');
22
25
  var location = input.val();
23
- window.googleAutocomplete = new GmapAutocomplete('#' + id, gmap);
24
- gmap.apply();
25
- googleAutocomplete.apply();
26
- if(location.length > 0){
27
- gmap.setMarker('address', location);
26
+ window.gmapAutocomplete = new GmapAutocomplete('#' + id, gmap);
27
+ if(#{apply.to_s}){
28
+ gmap.apply();
29
+ gmapAutocomplete.apply();
30
+ }
31
+ var latId = '#{lat_id.to_s}', lngId = '#{lng_id.to_s}';
32
+ if(latId && lngId){
33
+ gmap.setOptions({longitudeInput: lngId, latitudeInput: latId});
28
34
  }
29
35
  })"
30
36
  end
@@ -1,3 +1,3 @@
1
1
  module RailsGoogleMaps
2
- VERSION = "0.0.14"
2
+ VERSION = "0.0.15"
3
3
  end
@@ -48,8 +48,12 @@ class root.GmapAutocomplete extends Autocomplete
48
48
  self.gmap.update ui.item.geocode
49
49
 
50
50
  afterApply: ()->
51
+ this.syncWithMap()
51
52
  this._addKeyDownHandlers()
52
53
 
54
+ syncWithMap: ()->
55
+ @gmap.setMarker('address', $(@selector).val()) if $(@selector).val()
56
+
53
57
  _addKeyDownHandlers: ()->
54
58
  self = this
55
59
  $(@selector).bind 'keydown', (event)->
@@ -12,7 +12,7 @@ class root.GoogleMap
12
12
  mapSelector: "#gmaps-canvas"
13
13
  errorField: "#gmaps-error"
14
14
  onSucceed: []
15
-
15
+ mapless: false
16
16
  _applied: false
17
17
 
18
18
  @defaultGmapOptions: {
@@ -25,6 +25,9 @@ class root.GoogleMap
25
25
  # gmapOptions: google map api options
26
26
  constructor: (options = {}, gmapOptions = {})->
27
27
  @gmapOptions = $.extend {}, GoogleMap.defaultGmapOptions, gmapOptions
28
+ this.setOptions options
29
+
30
+ setOptions: (options)->
28
31
  @immutable = true if options['immutable']
29
32
  @mapSelector = options['selector'] if options['selector']
30
33
  @errorField = options['errorField'] if options['errorField']
@@ -33,10 +36,14 @@ class root.GoogleMap
33
36
  @longitudeInput = options['longitudeInput']
34
37
  @latitudeInput = options['latitudeInput']
35
38
 
39
+
36
40
  apply: ()->
37
- @map = new google.maps.Map $(@mapSelector)[0], @gmapOptions
41
+ if($(@mapSelector).length > 0)
42
+ @map = new google.maps.Map $(@mapSelector)[0], @gmapOptions
43
+ @marker = new google.maps.Marker {map: @map, draggable: true}
44
+ else
45
+ @mapless = true
38
46
  @geocoder = new google.maps.Geocoder()
39
- @marker = new google.maps.Marker {map: @map, draggable: true}
40
47
  @gmapErrors = new GmapErrors @errorField
41
48
  this._addListeners()
42
49
  @_applied = true
@@ -44,7 +51,7 @@ class root.GoogleMap
44
51
  applied: ()->
45
52
  @_applied
46
53
 
47
- setMarker: (type, value, update = true)->
54
+ setMarker: (type, value, update = true, afterCallback = ()->)->
48
55
  request = {}
49
56
  request[type] = value
50
57
  self = this
@@ -54,6 +61,7 @@ class root.GoogleMap
54
61
  self._succeed(results, update)
55
62
  else
56
63
  self._failed(update, type, value)
64
+ afterCallback()
57
65
 
58
66
  searchGeocodes: (term, callback)->
59
67
  @geocoder.geocode {'address': term }, (results, status)->
@@ -61,37 +69,44 @@ class root.GoogleMap
61
69
 
62
70
 
63
71
  update: (geocode)->
64
- @map.fitBounds geocode.geometry.viewport
65
- @marker.setPosition geocode.geometry.location
72
+ unless @mapless
73
+ @map.fitBounds geocode.geometry.viewport
74
+ @marker.setPosition geocode.geometry.location
75
+ this.saveLatLang(geocode.geometry) if @saveLangLat
66
76
 
67
77
  _addListeners: ()->
68
78
  self = this
69
79
  return if @immutable
70
- google.maps.event.addListener @marker, 'dragend', () ->
71
- self.setMarker 'latLng', self.marker.getPosition(), false
72
- google.maps.event.addListener @map, 'click', (event) ->
73
- self.marker.setPosition event.latLng
74
- self.setMarker 'latLng', event.latLng, false
80
+ unless @mapless
81
+ google.maps.event.addListener @marker, 'dragend', () ->
82
+ self.setMarker 'latLng', self.marker.getPosition(), false
83
+ google.maps.event.addListener @map, 'click', (event) ->
84
+ self.marker.setPosition event.latLng
85
+ self.setMarker 'latLng', event.latLng, false
75
86
 
76
87
  _succeed: (results, update)->
77
88
  if results[0]
78
89
  this.update(results[0]) if update
79
90
  $.map @onSucceed, (callback)->
80
91
  callback(results[0].formatted_address)
81
- this._saveLatLang(results[0].geometry.location.lat(), results[0].geometry.location.lng()) if @saveLangLat
92
+ this.saveLatLang(results[0].geometry) if @saveLangLat
82
93
  else
83
94
  @gmapErrors.wrongInputError()
84
95
 
85
96
  _failed: (update, type, value)->
86
- this._saveLatLang('', '') if @saveLangLat
97
+ this.clearLatLng() if @saveLangLat
87
98
  if type is 'address'
88
99
  @gmapErrors.incorrectAddress(value)
89
100
  else
90
101
  @gmapErrors.incorrectLatlng()
91
102
 
92
- _saveLatLang: (lat, long)->
93
- $(@latitudeInput).val(lat)
94
- $(@longitudeInput).val(long)
103
+ saveLatLang: (geometry)->
104
+ $(@latitudeInput).val(geometry.location.lat())
105
+ $(@longitudeInput).val(geometry.location.lng())
106
+
107
+ clearLatLng: ()->
108
+ $(@latitudeInput).val('')
109
+ $(@longitudeInput).val('')
95
110
 
96
111
  # Class for displaying Gmap Errors.
97
112
  # All the errors can be customised
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-google-maps
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.14
4
+ version: 0.0.15
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-04 00:00:00.000000000 Z
12
+ date: 2013-04-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails