|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Example: Address Completion on Google's Geocoding Service</title> |
|
|
6 |
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic"> |
|
|
7 |
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css"> |
|
|
8 |
<link rel="stylesheet" href="../assets/css/main.css"> |
|
|
9 |
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css"> |
|
|
10 |
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png"> |
|
|
11 |
<script src="../../build/yui/yui-min.js"></script> |
|
|
12 |
|
|
|
13 |
</head> |
|
|
14 |
<body> |
|
|
15 |
<!-- |
|
|
16 |
<a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a> |
|
|
17 |
--> |
|
|
18 |
<div id="doc"> |
|
|
19 |
<div id="hd"> |
|
|
20 |
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1> |
|
|
21 |
</div> |
|
|
22 |
|
|
|
23 |
|
|
|
24 |
<h1>Example: Address Completion on Google's Geocoding Service</h1> |
|
|
25 |
<div class="yui3-g"> |
|
|
26 |
<div class="yui3-u-3-4"> |
|
|
27 |
<div id="main"> |
|
|
28 |
<div class="content"><div class="intro"> |
|
|
29 |
<p> |
|
|
30 |
This example demonstrates how to provide AutoComplete suggestions for |
|
|
31 |
an address calling Google's Geocoding Service. |
|
|
32 |
</p> |
|
|
33 |
<p> |
|
|
34 |
Geocoding is the process of converting addresses into geographic |
|
|
35 |
coordinates. The <a href="https://developers.google.com/maps/documentation/geocoding/">Google Geocoding API</a> |
|
|
36 |
provides a direct way to access a geocoder via an HTTP request. |
|
|
37 |
</p> |
|
|
38 |
<p> |
|
|
39 |
Google's Geocoding Service returns a JSON object literal. The service does |
|
|
40 |
not provide a JSONP API. AutoComplete cannot use that object |
|
|
41 |
directly for security reasons. So the workaround is calling the service |
|
|
42 |
using YQL as source. |
|
|
43 |
</p> |
|
|
44 |
</div> |
|
|
45 |
|
|
|
46 |
<div class="example"> |
|
|
47 |
<div id="demo" class="yui3-skin-sam"> <!-- You need this skin class --> |
|
|
48 |
<p> |
|
|
49 |
<label for="ac-input">Enter an address:</label> |
|
|
50 |
<input id="ac-input" type="text" /> |
|
|
51 |
</p> |
|
|
52 |
|
|
|
53 |
<ul> |
|
|
54 |
<li>Latitude: <strong id="locationLat"></strong></li> |
|
|
55 |
<li>Longitude: <strong id="locationLng"></strong></li> |
|
|
56 |
</ul> |
|
|
57 |
</div> |
|
|
58 |
|
|
|
59 |
<script> |
|
|
60 |
YUI().use('autocomplete', function (Y) { |
|
|
61 |
var acNode = Y.one('#ac-input'); |
|
|
62 |
|
|
|
63 |
acNode.plug(Y.Plugin.AutoComplete, { |
|
|
64 |
// Highlight the first result of the list. |
|
|
65 |
activateFirstItem: true, |
|
|
66 |
|
|
|
67 |
// The list of the results contains up to 10 results. |
|
|
68 |
maxResults: 10, |
|
|
69 |
|
|
|
70 |
// To display the suggestions, the minimum of typed chars is five. |
|
|
71 |
minQueryLength: 5, |
|
|
72 |
|
|
|
73 |
// Number of milliseconds to wait after user input before triggering a |
|
|
74 |
// <code>query</code> event. This is useful to throttle queries to a remote data |
|
|
75 |
// source. |
|
|
76 |
queryDelay: 500, |
|
|
77 |
|
|
|
78 |
// Handling the list of results is mandatory, because the service can be |
|
|
79 |
// unavailable, can return an error, one result, or an array of results. |
|
|
80 |
// However <code>resultListLocator</code> needs to always return an array. |
|
|
81 |
resultListLocator: function (response) { |
|
|
82 |
// Makes sure an array is returned even on an error. |
|
|
83 |
if (response.error) { |
|
|
84 |
return []; |
|
|
85 |
} |
|
|
86 |
|
|
|
87 |
var query = response.query.results.json, |
|
|
88 |
addresses; |
|
|
89 |
|
|
|
90 |
if (query.status !== 'OK') { |
|
|
91 |
return []; |
|
|
92 |
} |
|
|
93 |
|
|
|
94 |
// Grab the actual addresses from the YQL query. |
|
|
95 |
addresses = query.results; |
|
|
96 |
|
|
|
97 |
// Makes sure an array is always returned. |
|
|
98 |
return addresses.length > 0 ? addresses : [addresses]; |
|
|
99 |
}, |
|
|
100 |
|
|
|
101 |
// When an item is selected, the value of the field indicated in the |
|
|
102 |
// <code>resultTextLocator</code> is displayed in the input field. |
|
|
103 |
resultTextLocator: 'formatted_address', |
|
|
104 |
|
|
|
105 |
// {query} placeholder is encoded, but to handle the spaces correctly, |
|
|
106 |
// the query is has to be encoded again: |
|
|
107 |
// |
|
|
108 |
// "my address" -> "my%2520address" // OK => {request} |
|
|
109 |
// "my address" -> "my%20address" // OK => {query} |
|
|
110 |
requestTemplate: function (query) { |
|
|
111 |
return encodeURI(query); |
|
|
112 |
}, |
|
|
113 |
|
|
|
114 |
// {request} placeholder, instead of the {query} one, this will insert |
|
|
115 |
// the <code>requestTemplate</code> value instead of the raw <code>query</code> value for |
|
|
116 |
// cases where you actually want a double-encoded (or customized) query. |
|
|
117 |
source: 'SELECT * FROM json WHERE ' + |
|
|
118 |
'url="http://maps.googleapis.com/maps/api/geocode/json?' + |
|
|
119 |
'sensor=false&' + |
|
|
120 |
'address={request}"', |
|
|
121 |
|
|
|
122 |
// Automatically adjust the width of the dropdown list. |
|
|
123 |
width: 'auto' |
|
|
124 |
}); |
|
|
125 |
|
|
|
126 |
// Adjust the width of the input container. |
|
|
127 |
acNode.ac.after('resultsChange', function () { |
|
|
128 |
var newWidth = this.get('boundingBox').get('offsetWidth'); |
|
|
129 |
acNode.setStyle('width', Math.max(newWidth, 100)); |
|
|
130 |
}); |
|
|
131 |
|
|
|
132 |
// Fill the <code>lat</code> and <code>lng</code> fields when the user selects an item. |
|
|
133 |
acNode.ac.on('select', function (e) { |
|
|
134 |
var location = e.result.raw.geometry.location; |
|
|
135 |
|
|
|
136 |
Y.one('#locationLat').set('text', location.lat); |
|
|
137 |
Y.one('#locationLng').set('text', location.lng); |
|
|
138 |
}); |
|
|
139 |
}); |
|
|
140 |
|
|
|
141 |
</script> |
|
|
142 |
</div> |
|
|
143 |
|
|
|
144 |
<h2>Complete Example Source</h2> |
|
|
145 |
|
|
|
146 |
<h3>JavaScript</h3> |
|
|
147 |
<pre class="code prettyprint">YUI().use('autocomplete', function (Y) { |
|
|
148 |
var acNode = Y.one('#ac-input'); |
|
|
149 |
|
|
|
150 |
acNode.plug(Y.Plugin.AutoComplete, { |
|
|
151 |
// Highlight the first result of the list. |
|
|
152 |
activateFirstItem: true, |
|
|
153 |
|
|
|
154 |
// The list of the results contains up to 10 results. |
|
|
155 |
maxResults: 10, |
|
|
156 |
|
|
|
157 |
// To display the suggestions, the minimum of typed chars is five. |
|
|
158 |
minQueryLength: 5, |
|
|
159 |
|
|
|
160 |
// Number of milliseconds to wait after user input before triggering a |
|
|
161 |
// `query` event. This is useful to throttle queries to a remote data |
|
|
162 |
// source. |
|
|
163 |
queryDelay: 500, |
|
|
164 |
|
|
|
165 |
// Handling the list of results is mandatory, because the service can be |
|
|
166 |
// unavailable, can return an error, one result, or an array of results. |
|
|
167 |
// However `resultListLocator` needs to always return an array. |
|
|
168 |
resultListLocator: function (response) { |
|
|
169 |
// Makes sure an array is returned even on an error. |
|
|
170 |
if (response.error) { |
|
|
171 |
return []; |
|
|
172 |
} |
|
|
173 |
|
|
|
174 |
var query = response.query.results.json, |
|
|
175 |
addresses; |
|
|
176 |
|
|
|
177 |
if (query.status !== 'OK') { |
|
|
178 |
return []; |
|
|
179 |
} |
|
|
180 |
|
|
|
181 |
// Grab the actual addresses from the YQL query. |
|
|
182 |
addresses = query.results; |
|
|
183 |
|
|
|
184 |
// Makes sure an array is always returned. |
|
|
185 |
return addresses.length > 0 ? addresses : [addresses]; |
|
|
186 |
}, |
|
|
187 |
|
|
|
188 |
// When an item is selected, the value of the field indicated in the |
|
|
189 |
// `resultTextLocator` is displayed in the input field. |
|
|
190 |
resultTextLocator: 'formatted_address', |
|
|
191 |
|
|
|
192 |
// {query} placeholder is encoded, but to handle the spaces correctly, |
|
|
193 |
// the query is has to be encoded again: |
|
|
194 |
// |
|
|
195 |
// "my address" -> "my%2520address" // OK => {request} |
|
|
196 |
// "my address" -> "my%20address" // OK => {query} |
|
|
197 |
requestTemplate: function (query) { |
|
|
198 |
return encodeURI(query); |
|
|
199 |
}, |
|
|
200 |
|
|
|
201 |
// {request} placeholder, instead of the {query} one, this will insert |
|
|
202 |
// the `requestTemplate` value instead of the raw `query` value for |
|
|
203 |
// cases where you actually want a double-encoded (or customized) query. |
|
|
204 |
source: 'SELECT * FROM json WHERE ' + |
|
|
205 |
'url="http://maps.googleapis.com/maps/api/geocode/json?' + |
|
|
206 |
'sensor=false&' + |
|
|
207 |
'address={request}"', |
|
|
208 |
|
|
|
209 |
// Automatically adjust the width of the dropdown list. |
|
|
210 |
width: 'auto' |
|
|
211 |
}); |
|
|
212 |
|
|
|
213 |
// Adjust the width of the input container. |
|
|
214 |
acNode.ac.after('resultsChange', function () { |
|
|
215 |
var newWidth = this.get('boundingBox').get('offsetWidth'); |
|
|
216 |
acNode.setStyle('width', Math.max(newWidth, 100)); |
|
|
217 |
}); |
|
|
218 |
|
|
|
219 |
// Fill the `lat` and `lng` fields when the user selects an item. |
|
|
220 |
acNode.ac.on('select', function (e) { |
|
|
221 |
var location = e.result.raw.geometry.location; |
|
|
222 |
|
|
|
223 |
Y.one('#locationLat').set('text', location.lat); |
|
|
224 |
Y.one('#locationLng').set('text', location.lng); |
|
|
225 |
}); |
|
|
226 |
});</pre> |
|
|
227 |
|
|
|
228 |
|
|
|
229 |
<h3>HTML</h3> |
|
|
230 |
<p> |
|
|
231 |
<strong>Note:</strong> be sure to add the <code>yui3-skin-sam</code> classname to the |
|
|
232 |
page's <code><body></code> element or to a parent element of the widget in order to apply |
|
|
233 |
the default CSS skin. See <a href="http://yuilibrary.com/yui/docs/tutorials/skins/">Understanding Skinning</a>. |
|
|
234 |
</p> |
|
|
235 |
<pre class="code prettyprint"><div id="demo" class="yui3-skin-sam"> <!-- You need this skin class --> |
|
|
236 |
<p> |
|
|
237 |
<label for="ac-input">Enter an address:</label> |
|
|
238 |
<input id="ac-input" type="text" /> |
|
|
239 |
</p> |
|
|
240 |
|
|
|
241 |
<ul> |
|
|
242 |
<li>Latitude: <strong id="locationLat"></strong></li> |
|
|
243 |
<li>Longitude: <strong id="locationLng"></strong></li> |
|
|
244 |
</ul> |
|
|
245 |
</div></pre> |
|
|
246 |
|
|
|
247 |
|
|
|
248 |
|
|
|
249 |
</div> |
|
|
250 |
</div> |
|
|
251 |
</div> |
|
|
252 |
|
|
|
253 |
<div class="yui3-u-1-4"> |
|
|
254 |
<div class="sidebar"> |
|
|
255 |
|
|
|
256 |
|
|
|
257 |
|
|
|
258 |
<div class="sidebox"> |
|
|
259 |
<div class="hd"> |
|
|
260 |
<h2 class="no-toc">Examples</h2> |
|
|
261 |
</div> |
|
|
262 |
|
|
|
263 |
<div class="bd"> |
|
|
264 |
<ul class="examples"> |
|
|
265 |
|
|
|
266 |
|
|
|
267 |
<li data-description="How to provide autocomplete suggestions from a local array."> |
|
|
268 |
<a href="ac-local.html">Basic Local Data</a> |
|
|
269 |
</li> |
|
|
270 |
|
|
|
271 |
|
|
|
272 |
|
|
|
273 |
<li data-description="How to provide autocomplete suggestions using a remote JSONP source."> |
|
|
274 |
<a href="ac-jsonp.html">Remote Data via JSONP</a> |
|
|
275 |
</li> |
|
|
276 |
|
|
|
277 |
|
|
|
278 |
|
|
|
279 |
<li data-description="How to provide autocomplete suggestions using a YQL query source."> |
|
|
280 |
<a href="ac-yql.html">Remote Data via YQL</a> |
|
|
281 |
</li> |
|
|
282 |
|
|
|
283 |
|
|
|
284 |
|
|
|
285 |
<li data-description="How to provide autocomplete suggestions using a DataSource instance."> |
|
|
286 |
<a href="ac-datasource.html">Remote Data via DataSource</a> |
|
|
287 |
</li> |
|
|
288 |
|
|
|
289 |
|
|
|
290 |
|
|
|
291 |
<li data-description="How to implement delimited tag completion."> |
|
|
292 |
<a href="ac-tagging.html">Tag Completion Using Query Delimiters</a> |
|
|
293 |
</li> |
|
|
294 |
|
|
|
295 |
|
|
|
296 |
|
|
|
297 |
<li data-description="How to find and select Flickr photos using a YQL source and a custom autocomplete result formatter."> |
|
|
298 |
<a href="ac-flickr.html">Find Photos on Flickr (Custom Formatting, YQL Source)</a> |
|
|
299 |
</li> |
|
|
300 |
|
|
|
301 |
|
|
|
302 |
|
|
|
303 |
<li data-description="How to use autocomplete-base to filter a set of existing items on a page."> |
|
|
304 |
<a href="ac-filter.html">Filter a Set of Existing Items on a Page</a> |
|
|
305 |
</li> |
|
|
306 |
|
|
|
307 |
|
|
|
308 |
|
|
|
309 |
<li data-description="How to find an address using a YQL source calling Google's Geocoding Service"> |
|
|
310 |
<a href="ac-geocode.html">Address Completion on Google's Geocoding Service</a> |
|
|
311 |
</li> |
|
|
312 |
|
|
|
313 |
|
|
|
314 |
</ul> |
|
|
315 |
</div> |
|
|
316 |
</div> |
|
|
317 |
|
|
|
318 |
|
|
|
319 |
|
|
|
320 |
</div> |
|
|
321 |
</div> |
|
|
322 |
</div> |
|
|
323 |
</div> |
|
|
324 |
|
|
|
325 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
326 |
<script>prettyPrint();</script> |
|
|
327 |
|
|
|
328 |
<script> |
|
|
329 |
YUI.Env.Tests = { |
|
|
330 |
examples: [], |
|
|
331 |
project: '../assets', |
|
|
332 |
assets: '../assets/autocomplete', |
|
|
333 |
name: 'ac-geocode', |
|
|
334 |
title: 'Address Completion on Google's Geocoding Service', |
|
|
335 |
newWindow: '', |
|
|
336 |
auto: false |
|
|
337 |
}; |
|
|
338 |
YUI.Env.Tests.examples.push('ac-local'); |
|
|
339 |
YUI.Env.Tests.examples.push('ac-jsonp'); |
|
|
340 |
YUI.Env.Tests.examples.push('ac-yql'); |
|
|
341 |
YUI.Env.Tests.examples.push('ac-datasource'); |
|
|
342 |
YUI.Env.Tests.examples.push('ac-tagging'); |
|
|
343 |
YUI.Env.Tests.examples.push('ac-flickr'); |
|
|
344 |
YUI.Env.Tests.examples.push('ac-filter'); |
|
|
345 |
YUI.Env.Tests.examples.push('ac-geocode'); |
|
|
346 |
|
|
|
347 |
</script> |
|
|
348 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
349 |
|
|
|
350 |
|
|
|
351 |
|
|
|
352 |
</body> |
|
|
353 |
</html> |