Built files from Bizgaze WebServer
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

jquery.geocomplete.js 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. /**
  2. * jQuery Geocoding and Places Autocomplete Plugin - V 1.7.0
  3. *
  4. * @author Martin Kleppe <kleppe@ubilabs.net>, 2016
  5. * @author Ubilabs http://ubilabs.net, 2016
  6. * @license MIT License <http://www.opensource.org/licenses/mit-license.php>
  7. */
  8. // # $.geocomplete()
  9. // ## jQuery Geocoding and Places Autocomplete Plugin
  10. //
  11. // * https://github.com/ubilabs/geocomplete/
  12. // * by Martin Kleppe <kleppe@ubilabs.net>
  13. (function ($, window, document, undefined) {
  14. // ## Options
  15. // The default options for this plugin.
  16. //
  17. // * `map` - Might be a selector, an jQuery object or a DOM element. Default is `false` which shows no map.
  18. // * `details` - The container that should be populated with data. Defaults to `false` which ignores the setting.
  19. // * 'detailsScope' - Allows you to scope the 'details' container and have multiple geocomplete fields on one page. Must be a parent of the input. Default is 'null'
  20. // * `location` - Location to initialize the map on. Might be an address `string` or an `array` with [latitude, longitude] or a `google.maps.LatLng`object. Default is `false` which shows a blank map.
  21. // * `bounds` - Whether to snap geocode search to map bounds. Default: `true` if false search globally. Alternatively pass a custom `LatLngBounds object.
  22. // * `autoselect` - Automatically selects the highlighted item or the first item from the suggestions list on Enter.
  23. // * `detailsAttribute` - The attribute's name to use as an indicator. Default: `"name"`
  24. // * `mapOptions` - Options to pass to the `google.maps.Map` constructor. See the full list [here](http://code.google.com/apis/maps/documentation/javascript/reference.html#MapOptions).
  25. // * `mapOptions.zoom` - The inital zoom level. Default: `14`
  26. // * `mapOptions.scrollwheel` - Whether to enable the scrollwheel to zoom the map. Default: `false`
  27. // * `mapOptions.mapTypeId` - The map type. Default: `"roadmap"`
  28. // * `markerOptions` - The options to pass to the `google.maps.Marker` constructor. See the full list [here](http://code.google.com/apis/maps/documentation/javascript/reference.html#MarkerOptions).
  29. // * `markerOptions.draggable` - If the marker is draggable. Default: `false`. Set to true to enable dragging.
  30. // * `markerOptions.disabled` - Do not show marker. Default: `false`. Set to true to disable marker.
  31. // * `maxZoom` - The maximum zoom level too zoom in after a geocoding response. Default: `16`
  32. // * `types` - An array containing one or more of the supported types for the places request. Default: `['geocode']` See the full list [here](http://code.google.com/apis/maps/documentation/javascript/places.html#place_search_requests).
  33. // * `blur` - Trigger geocode when input loses focus.
  34. // * `geocodeAfterResult` - If blur is set to true, choose whether to geocode if user has explicitly selected a result before blur.
  35. // * `restoreValueAfterBlur` - Restores the input's value upon blurring. Default is `false` which ignores the setting.
  36. var defaults = {
  37. bounds: true,
  38. country: null,
  39. map: false,
  40. details: false,
  41. detailsAttribute: "name",
  42. detailsScope: null,
  43. autoselect: true,
  44. location: false,
  45. mapOptions: {
  46. zoom: 14,
  47. scrollwheel: true,
  48. mapTypeId: "roadmap"
  49. },
  50. markerOptions: {
  51. draggable: false
  52. },
  53. maxZoom: 16,
  54. types: ['geocode'],
  55. blur: false,
  56. geocodeAfterResult: false,
  57. restoreValueAfterBlur: false
  58. };
  59. // See: [Geocoding Types](https://developers.google.com/maps/documentation/geocoding/#Types)
  60. // on Google Developers.
  61. var componentTypes = ("street_address route intersection political " +
  62. "country administrative_area_level_1 administrative_area_level_2 " +
  63. "administrative_area_level_3 colloquial_area locality sublocality " +
  64. "neighborhood premise subpremise postal_code natural_feature airport " +
  65. "park point_of_interest post_box street_number floor room " +
  66. "lat lng viewport location " +
  67. "formatted_address location_type bounds").split(" ");
  68. // See: [Places Details Responses](https://developers.google.com/maps/documentation/javascript/places#place_details_responses)
  69. // on Google Developers.
  70. var placesDetails = ("id place_id url website vicinity reference name rating " +
  71. "international_phone_number icon formatted_phone_number").split(" ");
  72. // The actual plugin constructor.
  73. function GeoComplete(input, options) {
  74. this.options = $.extend(true, {}, defaults, options);
  75. // This is a fix to allow types:[] not to be overridden by defaults
  76. // so search results includes everything
  77. if (options && options.types) {
  78. this.options.types = options.types;
  79. }
  80. this.input = input;
  81. this.$input = $(input);
  82. this._defaults = defaults;
  83. this._name = 'geocomplete';
  84. this.init();
  85. }
  86. // Initialize all parts of the plugin.
  87. $.extend(GeoComplete.prototype, {
  88. init: function () {
  89. this.initMap();
  90. this.initMarker();
  91. this.initGeocoder();
  92. this.initDetails();
  93. this.initLocation();
  94. },
  95. // Initialize the map but only if the option `map` was set.
  96. // This will create a `map` within the given container
  97. // using the provided `mapOptions` or link to the existing map instance.
  98. initMap: function () {
  99. if (!this.options.map) { return; }
  100. if (typeof this.options.map.setCenter == "function") {
  101. this.map = this.options.map;
  102. return;
  103. }
  104. this.map = new google.maps.Map(
  105. $(this.options.map)[0],
  106. this.options.mapOptions
  107. );
  108. // add click event listener on the map
  109. google.maps.event.addListener(
  110. this.map,
  111. 'click',
  112. $.proxy(this.mapClicked, this)
  113. );
  114. // add dragend even listener on the map
  115. google.maps.event.addListener(
  116. this.map,
  117. 'dragend',
  118. $.proxy(this.mapDragged, this)
  119. );
  120. // add idle even listener on the map
  121. google.maps.event.addListener(
  122. this.map,
  123. 'idle',
  124. $.proxy(this.mapIdle, this)
  125. );
  126. google.maps.event.addListener(
  127. this.map,
  128. 'zoom_changed',
  129. $.proxy(this.mapZoomed, this)
  130. );
  131. },
  132. // Add a marker with the provided `markerOptions` but only
  133. // if the option was set. Additionally it listens for the `dragend` event
  134. // to notify the plugin about changes.
  135. initMarker: function () {
  136. if (!this.map) { return; }
  137. var options = $.extend(this.options.markerOptions, { map: this.map });
  138. if (options.disabled) { return; }
  139. this.marker = new google.maps.Marker(options);
  140. google.maps.event.addListener(
  141. this.marker,
  142. 'dragend',
  143. $.proxy(this.markerDragged, this)
  144. );
  145. },
  146. // Associate the input with the autocompleter and create a geocoder
  147. // to fall back when the autocompleter does not return a value.
  148. initGeocoder: function () {
  149. // Indicates is user did select a result from the dropdown.
  150. var selected = false;
  151. var options = {
  152. types: this.options.types,
  153. bounds: this.options.bounds === true ? null : this.options.bounds,
  154. componentRestrictions: this.options.componentRestrictions
  155. };
  156. if (this.options.country) {
  157. options.componentRestrictions = { country: this.options.country };
  158. }
  159. this.autocomplete = new google.maps.places.Autocomplete(
  160. this.input, options
  161. );
  162. this.geocoder = new google.maps.Geocoder();
  163. // Bind autocomplete to map bounds but only if there is a map
  164. // and `options.bindToMap` is set to true.
  165. if (this.map && this.options.bounds === true) {
  166. this.autocomplete.bindTo('bounds', this.map);
  167. }
  168. // Watch `place_changed` events on the autocomplete input field.
  169. google.maps.event.addListener(
  170. this.autocomplete,
  171. 'place_changed',
  172. $.proxy(this.placeChanged, this)
  173. );
  174. // Prevent parent form from being submitted if user hit enter.
  175. this.$input.on('keypress.' + this._name, function (event) {
  176. if (event.keyCode === 13) { return false; }
  177. });
  178. // Assume that if user types anything after having selected a result,
  179. // the selected location is not valid any more.
  180. if (this.options.geocodeAfterResult === true) {
  181. this.$input.bind('keypress.' + this._name, $.proxy(function () {
  182. if (event.keyCode != 9 && this.selected === true) {
  183. this.selected = false;
  184. }
  185. }, this));
  186. }
  187. // Listen for "geocode" events and trigger find action.
  188. this.$input.bind('geocode.' + this._name, $.proxy(function () {
  189. this.find();
  190. }, this));
  191. // Saves the previous input value
  192. this.$input.bind('geocode:result.' + this._name, $.proxy(function () {
  193. this.lastInputVal = this.$input.val();
  194. }, this));
  195. // Trigger find action when input element is blurred out and user has
  196. // not explicitly selected a result.
  197. // (Useful for typing partial location and tabbing to the next field
  198. // or clicking somewhere else.)
  199. if (this.options.blur === true) {
  200. this.$input.on('blur.' + this._name, $.proxy(function () {
  201. if (this.options.geocodeAfterResult === true && this.selected === true) { return; }
  202. if (this.options.restoreValueAfterBlur === true && this.selected === true) {
  203. setTimeout($.proxy(this.restoreLastValue, this), 0);
  204. } else {
  205. this.find();
  206. }
  207. }, this));
  208. }
  209. },
  210. // Prepare a given DOM structure to be populated when we got some data.
  211. // This will cycle through the list of component types and map the
  212. // corresponding elements.
  213. initDetails: function () {
  214. if (!this.options.details) { return; }
  215. if (this.options.detailsScope) {
  216. var $details = $(this.input).parents(this.options.detailsScope).find(this.options.details);
  217. } else {
  218. var $details = $(this.options.details);
  219. }
  220. var attribute = this.options.detailsAttribute,
  221. details = {};
  222. function setDetail(value) {
  223. details[value] = $details.find("[" + attribute + "=" + value + "]");
  224. }
  225. $.each(componentTypes, function (index, key) {
  226. setDetail(key);
  227. setDetail(key + "_short");
  228. });
  229. $.each(placesDetails, function (index, key) {
  230. setDetail(key);
  231. });
  232. this.$details = $details;
  233. this.details = details;
  234. },
  235. // Set the initial location of the plugin if the `location` options was set.
  236. // This method will care about converting the value into the right format.
  237. initLocation: function () {
  238. var location = this.options.location, latLng;
  239. if (!location) { return; }
  240. if (typeof location == 'string') {
  241. this.find(location);
  242. return;
  243. }
  244. if (location instanceof Array) {
  245. latLng = new google.maps.LatLng(location[0], location[1]);
  246. }
  247. if (location instanceof google.maps.LatLng) {
  248. latLng = location;
  249. }
  250. if (latLng) {
  251. if (this.map) { this.map.setCenter(latLng); }
  252. if (this.marker) { this.marker.setPosition(latLng); }
  253. }
  254. },
  255. destroy: function () {
  256. if (this.map) {
  257. google.maps.event.clearInstanceListeners(this.map);
  258. google.maps.event.clearInstanceListeners(this.marker);
  259. }
  260. this.autocomplete.unbindAll();
  261. google.maps.event.clearInstanceListeners(this.autocomplete);
  262. google.maps.event.clearInstanceListeners(this.input);
  263. this.$input.removeData();
  264. this.$input.off(this._name);
  265. this.$input.unbind('.' + this._name);
  266. },
  267. // Look up a given address. If no `address` was specified it uses
  268. // the current value of the input.
  269. find: function (address) {
  270. this.geocode({
  271. address: address || this.$input.val()
  272. });
  273. },
  274. // Requests details about a given location.
  275. // Additionally it will bias the requests to the provided bounds.
  276. geocode: function (request) {
  277. // Don't geocode if the requested address is empty
  278. if (!request.address) {
  279. return;
  280. }
  281. if (this.options.bounds && !request.bounds) {
  282. if (this.options.bounds === true) {
  283. request.bounds = this.map && this.map.getBounds();
  284. } else {
  285. request.bounds = this.options.bounds;
  286. }
  287. }
  288. if (this.options.country) {
  289. request.region = this.options.country;
  290. }
  291. this.geocoder.geocode(request, $.proxy(this.handleGeocode, this));
  292. },
  293. // Get the selected result. If no result is selected on the list, then get
  294. // the first result from the list.
  295. selectFirstResult: function () {
  296. //$(".pac-container").hide();
  297. var selected = '';
  298. // Check if any result is selected.
  299. if ($(".pac-item-selected")[0]) {
  300. selected = '-selected';
  301. }
  302. // Get the first suggestion's text.
  303. var $span1 = $(".pac-container:visible .pac-item" + selected + ":first span:nth-child(2)").text();
  304. var $span2 = $(".pac-container:visible .pac-item" + selected + ":first span:nth-child(3)").text();
  305. // Adds the additional information, if available.
  306. var firstResult = $span1;
  307. if ($span2) {
  308. firstResult += " - " + $span2;
  309. }
  310. this.$input.val(firstResult);
  311. return firstResult;
  312. },
  313. // Restores the input value using the previous value if it exists
  314. restoreLastValue: function () {
  315. if (this.lastInputVal) { this.$input.val(this.lastInputVal); }
  316. },
  317. // Handles the geocode response. If more than one results was found
  318. // it triggers the "geocode:multiple" events. If there was an error
  319. // the "geocode:error" event is fired.
  320. handleGeocode: function (results, status) {
  321. if (status === google.maps.GeocoderStatus.OK) {
  322. var result = results[0];
  323. this.$input.val(result.formatted_address);
  324. this.update(result);
  325. if (results.length > 1) {
  326. this.trigger("geocode:multiple", results);
  327. }
  328. } else {
  329. this.trigger("geocode:error", status);
  330. }
  331. },
  332. // Triggers a given `event` with optional `arguments` on the input.
  333. trigger: function (event, argument) {
  334. this.$input.trigger(event, [argument]);
  335. },
  336. // Set the map to a new center by passing a `geometry`.
  337. // If the geometry has a viewport, the map zooms out to fit the bounds.
  338. // Additionally it updates the marker position.
  339. center: function (geometry) {
  340. if (geometry.viewport) {
  341. this.map.fitBounds(geometry.viewport);
  342. if (this.map.getZoom() > this.options.maxZoom) {
  343. this.map.setZoom(this.options.maxZoom);
  344. }
  345. } else {
  346. this.map.setZoom(this.options.maxZoom);
  347. this.map.setCenter(geometry.location);
  348. }
  349. if (this.marker) {
  350. this.marker.setPosition(geometry.location);
  351. this.marker.setAnimation(this.options.markerOptions.animation);
  352. }
  353. },
  354. // Update the elements based on a single places or geocoding response
  355. // and trigger the "geocode:result" event on the input.
  356. update: function (result) {
  357. if (this.map) {
  358. this.center(result.geometry);
  359. }
  360. if (this.$details) {
  361. this.fillDetails(result);
  362. }
  363. this.trigger("geocode:result", result);
  364. },
  365. // Populate the provided elements with new `result` data.
  366. // This will lookup all elements that has an attribute with the given
  367. // component type.
  368. fillDetails: function (result) {
  369. var data = {},
  370. geometry = result.geometry,
  371. viewport = geometry.viewport,
  372. bounds = geometry.bounds;
  373. // Create a simplified version of the address components.
  374. $.each(result.address_components, function (index, object) {
  375. var name = object.types[0];
  376. $.each(object.types, function (index, name) {
  377. data[name] = object.long_name;
  378. data[name + "_short"] = object.short_name;
  379. });
  380. });
  381. // Add properties of the places details.
  382. $.each(placesDetails, function (index, key) {
  383. data[key] = result[key];
  384. });
  385. // Add infos about the address and geometry.
  386. $.extend(data, {
  387. formatted_address: result.formatted_address,
  388. location_type: geometry.location_type || "PLACES",
  389. viewport: viewport,
  390. bounds: bounds,
  391. location: geometry.location,
  392. lat: geometry.location.lat(),
  393. lng: geometry.location.lng()
  394. });
  395. // Set the values for all details.
  396. $.each(this.details, $.proxy(function (key, $detail) {
  397. var value = data[key];
  398. this.setDetail($detail, value);
  399. }, this));
  400. this.data = data;
  401. },
  402. // Assign a given `value` to a single `$element`.
  403. // If the element is an input, the value is set, otherwise it updates
  404. // the text content.
  405. setDetail: function ($element, value) {
  406. if (value === undefined) {
  407. value = "";
  408. } else if (typeof value.toUrlValue == "function") {
  409. value = value.toUrlValue();
  410. }
  411. if ($element.is(":input")) {
  412. $element.val(value);
  413. } else {
  414. $element.text(value);
  415. }
  416. },
  417. // Fire the "geocode:dragged" event and pass the new position.
  418. markerDragged: function (event) {
  419. this.trigger("geocode:dragged", event.latLng);
  420. },
  421. markerPositionChanged: function (event) {
  422. this.trigger("geocode:dragged", event.latLng);
  423. },
  424. mapClicked: function (event) {
  425. this.trigger("geocode:click", event.latLng);
  426. },
  427. // Fire the "geocode:mapdragged" event and pass the current position of the map center.
  428. mapDragged: function (event) {
  429. this.trigger("geocode:mapdragged", this.map.getCenter());
  430. },
  431. // Fire the "geocode:idle" event and pass the current position of the map center.
  432. mapIdle: function (event) {
  433. this.trigger("geocode:idle", this.map.getCenter());
  434. },
  435. mapZoomed: function (event) {
  436. this.trigger("geocode:zoom", this.map.getZoom());
  437. },
  438. // Restore the old position of the marker to the last knwon location.
  439. resetMarker: function () {
  440. this.marker.setPosition(this.data.location);
  441. this.setDetail(this.details.lat, this.data.location.lat());
  442. this.setDetail(this.details.lng, this.data.location.lng());
  443. },
  444. // Update the plugin after the user has selected an autocomplete entry.
  445. // If the place has no geometry it passes it to the geocoder.
  446. placeChanged: function () {
  447. var place = this.autocomplete.getPlace();
  448. this.selected = true;
  449. if (!place.geometry) {
  450. if (this.options.autoselect) {
  451. // Automatically selects the highlighted item or the first item from the
  452. // suggestions list.
  453. var autoSelection = this.selectFirstResult();
  454. this.find(autoSelection);
  455. }
  456. } else {
  457. // Use the input text if it already gives geometry.
  458. this.update(place);
  459. }
  460. }
  461. });
  462. // A plugin wrapper around the constructor.
  463. // Pass `options` with all settings that are different from the default.
  464. // The attribute is used to prevent multiple instantiations of the plugin.
  465. $.fn.geocomplete = function (options) {
  466. var attribute = 'plugin_geocomplete';
  467. // If you call `.geocomplete()` with a string as the first parameter
  468. // it returns the corresponding property or calls the method with the
  469. // following arguments.
  470. if (typeof options == "string") {
  471. var instance = $(this).data(attribute) || $(this).geocomplete().data(attribute),
  472. prop = instance[options];
  473. if (typeof prop == "function") {
  474. prop.apply(instance, Array.prototype.slice.call(arguments, 1));
  475. return $(this);
  476. } else {
  477. if (arguments.length == 2) {
  478. prop = arguments[1];
  479. }
  480. return prop;
  481. }
  482. } else {
  483. return this.each(function () {
  484. // Prevent against multiple instantiations.
  485. var instance = $.data(this, attribute);
  486. if (!instance) {
  487. instance = new GeoComplete(this, options);
  488. $.data(this, attribute, instance);
  489. }
  490. });
  491. }
  492. };
  493. })(jQuery, window, document);