gmaps-autocomplete-rails 0.1.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.
@@ -0,0 +1,6 @@
1
+
2
+ .ui-corner-all, .ui-corner-top, .ui-corner-right, .ui-corner-left, .ui-corner-bottom{ border-radius:0px;}
3
+ .ui-state-active,.ui-tabs-selected { border-radius:0px;}
4
+ .ui-tabs-selected { border-radius:0px;}
5
+ .ui-tabs .ui-tabs-nav li{ filter:none;}
6
+ .ui-tabs .ui-tabs-nav li a { border-radius:0px; }
data/spec/main.css ADDED
@@ -0,0 +1,89 @@
1
+ html {
2
+ margin:0;
3
+ padding:0;
4
+ border:0;
5
+ }
6
+
7
+ body, div, span, object, iframe,
8
+ h1, h2, h3, h4, h5, h6, p, blockquote, pre,
9
+ a, abbr, acronym, address, code,
10
+ del, dfn, em, img, q, dl, dt, dd, ol, ul, li,
11
+ fieldset, form, label, legend,
12
+ table, caption, tbody, tfoot, thead, tr, th, td,
13
+ article, aside, dialog, figure, footer, header,
14
+ hgroup, nav, section {
15
+ margin: 0;
16
+ padding: 0;
17
+ border: 0;
18
+ font-size: 100%;
19
+ font: inherit;
20
+ vertical-align: baseline;
21
+ }
22
+
23
+ body {
24
+ color: #333;
25
+ margin: 50px;
26
+ font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size:13px;
27
+ }
28
+
29
+ #gmaps-canvas {
30
+ height: 400px;
31
+
32
+ border: 1px solid #999;
33
+ -moz-box-shadow: 0px 0px 5px #ccc;
34
+ -webkit-box-shadow: 0px 0px 5px #ccc;
35
+ box-shadow: 0px 0px 5px #ccc;
36
+ }
37
+
38
+ #gmaps-error {
39
+ color: #cc0000;
40
+ }
41
+
42
+ h1 {
43
+ font-weight: bold;
44
+ padding-top: 5px;
45
+ padding-bottom: 5px;
46
+ }
47
+
48
+ p {
49
+ padding-top: 5px;
50
+ padding-bottom: 5px;
51
+ }
52
+
53
+ hr {
54
+ border: none;
55
+ padding: 5px;
56
+ border-bottom: 1px solid #efefef;
57
+ }
58
+
59
+ #container {
60
+ width: 80%;
61
+ margin-left: auto;
62
+ margin-right: auto;
63
+ margin-top: 30px;
64
+ padding: 25px;
65
+ border: 1px solid #999;
66
+ -moz-box-shadow: 0px 0px 15px #ccc;
67
+ -webkit-box-shadow: 0px 0px 15px #ccc;
68
+ box-shadow: 0px 0px 15px #ccc;
69
+ }
70
+
71
+ input {
72
+ width: 500px;
73
+ }
74
+
75
+ #results {
76
+ color: #555;
77
+ padding-top: 10px;
78
+ }
79
+
80
+ #input {
81
+ padding-top: 20px;
82
+ padding-bottom: 20px;
83
+ color: #ccc;
84
+ }
85
+ #input #gmaps-output-latitude, #input #gmaps-output-longitude {
86
+ color: green;
87
+ }
88
+
89
+ .download { float: right; }
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'gmaps-autocomplete-rails'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
@@ -0,0 +1,233 @@
1
+ var GmapsAutoComplete = {
2
+ geocoder: null,
3
+ map: null,
4
+ marker: null,
5
+ inputField: null,
6
+ errorField: null,
7
+ positionOutputter: null,
8
+
9
+ // initialise the google maps objects, and add listeners
10
+ mapElem: document.getElementById("gmaps-canvas"),
11
+ zoomLevel: 2,
12
+ mapType: google.maps.MapTypeId.ROADMAP,
13
+ pos: [51.751724, -1.255284],
14
+ inputField: '#gmaps-input-address',
15
+ errorField: '#gmaps-error',
16
+ positionOutputter: this.defaultPositionOutputter,
17
+
18
+ gmaps_init: function(opts){
19
+ opts = opts || {};
20
+
21
+ defaultOptions = {
22
+ mapElem: "#gmaps-canvas",
23
+ zoomLevel: 2,
24
+ mapType: google.maps.MapTypeId.ROADMAP,
25
+ pos: [51.751724, -1.255284],
26
+ inputField: '#gmaps-input-address',
27
+ errorField: '#gmaps-error',
28
+ positionOutputter: this.defaultPositionOutputter
29
+ };
30
+
31
+ $.extend(opts, defaultOptions);
32
+
33
+ var pos = opts['pos'];
34
+ var lat = pos[0];
35
+ var lng = pos[1];
36
+
37
+ var mapType = opts['mapType'];
38
+ var mapElem = null;
39
+
40
+ if (opts['mapElem']) {
41
+ mapElem = $(opts['mapElem']).get(0);
42
+ }
43
+
44
+ var zoomLevel = opts['zoomLevel'];
45
+
46
+ this.inputField = opts['inputField'];
47
+ this.errorField = opts['#gmaps-error'];
48
+
49
+ this.positionOutputter = opts['positionOutputter'];
50
+
51
+ // center of the universe
52
+ var latlng = new google.maps.LatLng(lat, lng);
53
+
54
+ var options = {
55
+ zoom: zoomLevel,
56
+ center: latlng,
57
+ mapTypeId: mapType
58
+ };
59
+
60
+ // the geocoder object allows us to do latlng lookup based on address
61
+ geocoder = new google.maps.Geocoder();
62
+
63
+ var self = this;
64
+
65
+ if (typeof(mapElem) == 'undefined') {
66
+ self.showError("Map element " + opts['mapElem'] + " could not be resolved!");
67
+ }
68
+
69
+ if (mapElem) {
70
+ // create our map object
71
+ this.map = new google.maps.Map(mapElem, options);
72
+
73
+ if (this.map) {
74
+ // the marker shows us the position of the latest address
75
+ this.marker = new google.maps.Marker({
76
+ map: this.map,
77
+ draggable: true
78
+ });
79
+
80
+ marker = this.marker;
81
+
82
+ // event triggered when marker is dragged and dropped
83
+ google.maps.event.addListener(this.marker, 'dragend', function() {
84
+ self.geocode_lookup( 'latLng', marker.getPosition() );
85
+ });
86
+
87
+ // event triggered when map is clicked
88
+ google.maps.event.addListener(this.map, 'click', function(event) {
89
+ marker.setPosition(event.latLng)
90
+ self.geocode_lookup( 'latLng', event.latLng );
91
+ });
92
+ }
93
+ }
94
+ },
95
+
96
+ // move the marker to a new position, and center the map on it
97
+ update_map: function( geometry ) {
98
+ if (this.map) {
99
+ this.map.fitBounds( geometry.viewport )
100
+ }
101
+
102
+ if (this.marker) {
103
+ this.marker.setPosition( geometry.location )
104
+ }
105
+ },
106
+
107
+ // fill in the UI elements with new position data
108
+ update_ui: function( address, latLng ) {
109
+ $(this.inputField).autocomplete("close");
110
+ $(this.inputField).val(address);
111
+
112
+ this.positionOutputter(latLng);
113
+ },
114
+
115
+ defaultPositionOutputter: function(latLng) {
116
+ $('#gmaps-output-latitude').html(latLng.lat());
117
+ $('#gmaps-output-longitude').html(latLng.lng());
118
+ },
119
+
120
+ // Query the Google geocode object
121
+ //
122
+ // type: 'address' for search by address
123
+ // 'latLng' for search by latLng (reverse lookup)
124
+ //
125
+ // value: search query
126
+ //
127
+ // update: should we update the map (center map and position marker)?
128
+ geocode_lookup: function( type, value, update ) {
129
+ // default value: update = false
130
+ update = typeof update !== 'undefined' ? update : false;
131
+
132
+ request = {};
133
+ request[type] = value;
134
+ var self = this;
135
+
136
+ geocoder.geocode({address: request.term, region: 'DK'}, function(results, status) {
137
+ $(self.errorField).html('');
138
+ if (status == google.maps.GeocoderStatus.OK) {
139
+ // Google geocoding has succeeded!
140
+ if (results[0]) {
141
+ // Always update the UI elements with new location data
142
+ self.update_ui( results[0].formatted_address,
143
+ results[0].geometry.location )
144
+
145
+ // Only update the map (position marker and center map) if requested
146
+ if( update ) { update_map( results[0].geometry ) }
147
+ } else {
148
+ // Geocoder status ok but no results!?
149
+ self.showError("Sorry, something went wrong. Try again!");
150
+ }
151
+ } else {
152
+ // Google Geocoding has failed. Two common reasons:
153
+ // * Address not recognised (e.g. search for 'zxxzcxczxcx')
154
+ // * Location doesn't map to address (e.g. click in middle of Atlantic)
155
+
156
+ if( type == 'address' ) {
157
+ // User has typed in an address which we can't geocode to a location
158
+ self.showError("Sorry! We couldn't find " + value + ". Try a different search term, or click the map." );
159
+ } else {
160
+ // User has clicked or dragged marker to somewhere that Google can't do a reverse lookup for
161
+ // In this case we display a warning, clear the address box, but fill in LatLng
162
+ self.showError("Woah... that's pretty remote! You're going to have to manually enter a place name." );
163
+ self.update_ui('', value)
164
+ }
165
+ };
166
+ });
167
+ },
168
+
169
+ showError: function(msg) {
170
+ $(self.errorField).html(msg);
171
+ $(self.errorField).show();
172
+
173
+ setTimeout(function(){
174
+ $(self.errorField).hide();
175
+ }, 1000);
176
+ },
177
+
178
+ // initialise the jqueryUI autocomplete element
179
+ autocomplete_init: function (opts) {
180
+ var self = this;
181
+ opts = opts || {};
182
+ var region = opts['region'] || 'DK';
183
+
184
+ $(self.inputField).autocomplete({
185
+
186
+ // source is the list of input options shown in the autocomplete dropdown.
187
+ // see documentation: http://jqueryui.com/demos/autocomplete/
188
+ source: function(request,response) {
189
+
190
+ // https://developers.google.com/maps/documentation/geocoding/#RegionCodes
191
+ console.log('region', region);
192
+ var region_postfix = ''
193
+ if (region) {
194
+ region_postfix = ', ' + region
195
+ }
196
+
197
+ geocode_opts = {'address': request.term + region_postfix }
198
+
199
+ // the geocode method takes an address or LatLng to search for
200
+ // and a callback function which should process the results into
201
+ // a format accepted by jqueryUI autocomplete
202
+ geocoder.geocode(geocode_opts, function(results, status) {
203
+ response($.map(results, function(item) {
204
+ return {
205
+ label: item.formatted_address, // appears in dropdown box
206
+ value: item.formatted_address, // inserted into input element when selected
207
+ geocode: item // all geocode data: used in select callback event
208
+ }
209
+ }));
210
+ })
211
+ },
212
+
213
+ // event triggered when drop-down option selected
214
+ select: function(event,ui){
215
+ self.update_ui( ui.item.value, ui.item.geocode.geometry.location )
216
+ self.update_map( ui.item.geocode.geometry )
217
+ }
218
+ });
219
+
220
+ // triggered when user presses a key in the address box
221
+ $(self.inputField).bind('keydown', function(event) {
222
+ if(event.keyCode == 13) {
223
+ geocode_lookup( 'address', $(self.inputField).val(), true );
224
+
225
+ // ensures dropdown disappears when enter is pressed
226
+ $(self.inputField).autocomplete("disable")
227
+ } else {
228
+ // re-enable if previously disabled above
229
+ $(self.inputField).autocomplete("enable")
230
+ }
231
+ });
232
+ } // autocomplete_init
233
+ }
metadata ADDED
@@ -0,0 +1,178 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gmaps-autocomplete-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kristian Mandrup
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 2.8.0
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 2.8.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: rdoc
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '3.12'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '3.12'
62
+ - !ruby/object:Gem::Dependency
63
+ name: bundler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: 1.0.0
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: 1.0.0
78
+ - !ruby/object:Gem::Dependency
79
+ name: jeweler
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: 1.8.4
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 1.8.4
94
+ - !ruby/object:Gem::Dependency
95
+ name: simplecov
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0.5'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0.5'
110
+ description: Easily add autocomplete geo-functionality to your Rails app :)
111
+ email: kmandrup@gmail.com
112
+ executables: []
113
+ extensions: []
114
+ extra_rdoc_files:
115
+ - LICENSE.txt
116
+ - README.md
117
+ files:
118
+ - .document
119
+ - .rspec
120
+ - Gemfile
121
+ - Gemfile.lock
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - VERSION
126
+ - lib/gmaps-autocomplete-rails.rb
127
+ - lib/gmaps-autocomplete/view_helper.rb
128
+ - spec/index.html
129
+ - spec/init.js
130
+ - spec/jquery-ui/images/ui-bg_flat_0_aaaaaa_40x100.png
131
+ - spec/jquery-ui/images/ui-bg_glass_55_fbf9ee_1x400.png
132
+ - spec/jquery-ui/images/ui-bg_glass_65_ffffff_1x400.png
133
+ - spec/jquery-ui/images/ui-bg_glass_75_dadada_1x400.png
134
+ - spec/jquery-ui/images/ui-bg_glass_75_e6e6e6_1x400.png
135
+ - spec/jquery-ui/images/ui-bg_glass_75_ffffff_1x400.png
136
+ - spec/jquery-ui/images/ui-bg_highlight-soft_75_cccccc_1x100.png
137
+ - spec/jquery-ui/images/ui-bg_inset-soft_95_fef1ec_1x100.png
138
+ - spec/jquery-ui/images/ui-icons_222222_256x240.png
139
+ - spec/jquery-ui/images/ui-icons_2e83ff_256x240.png
140
+ - spec/jquery-ui/images/ui-icons_454545_256x240.png
141
+ - spec/jquery-ui/images/ui-icons_888888_256x240.png
142
+ - spec/jquery-ui/images/ui-icons_cd0a0a_256x240.png
143
+ - spec/jquery-ui/images/ui-icons_f6cf3b_256x240.png
144
+ - spec/jquery-ui/jquery-ui-1.8.16.custom.css
145
+ - spec/jquery-ui/jquery.ui.1.8.16.ie.css
146
+ - spec/main.css
147
+ - spec/spec_helper.rb
148
+ - vendor/assets/javascripts/gmaps-autocomplete.js
149
+ homepage: http://github.com/kristianmandrup/gmaps-autocomplete-rails
150
+ licenses:
151
+ - MIT
152
+ post_install_message:
153
+ rdoc_options: []
154
+ require_paths:
155
+ - lib
156
+ required_ruby_version: !ruby/object:Gem::Requirement
157
+ none: false
158
+ requirements:
159
+ - - ! '>='
160
+ - !ruby/object:Gem::Version
161
+ version: '0'
162
+ segments:
163
+ - 0
164
+ hash: 2031419109795997149
165
+ required_rubygems_version: !ruby/object:Gem::Requirement
166
+ none: false
167
+ requirements:
168
+ - - ! '>='
169
+ - !ruby/object:Gem::Version
170
+ version: '0'
171
+ requirements: []
172
+ rubyforge_project:
173
+ rubygems_version: 1.8.24
174
+ signing_key:
175
+ specification_version: 3
176
+ summary: Google Maps v3 search with jQuery UI Autocomplete, ready for use with Rails
177
+ asset pipeline
178
+ test_files: []