Guest User

Untitled

a guest
Jul 19th, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. // This example displays an address form, using the autocomplete feature
  2. // of the Google Places API to help users fill in the information.
  3.  
  4. var placeSearch, autocomplete;
  5. var componentForm = {
  6. street_number: 'short_name',
  7. route: 'long_name',
  8. locality: 'long_name',
  9. administrative_area_level_1: 'short_name',
  10. country: 'long_name',
  11. postal_code: 'short_name'
  12. };
  13.  
  14. function initAutocomplete() {
  15. // Create the autocomplete object, restricting the search to geographical
  16. // location types.
  17. autocomplete = new google.maps.places.Autocomplete(
  18. /** @type {!HTMLInputElement} */(document.getElementById('autocomplete')),
  19. {types: ['geocode']});
  20.  
  21. // When the user selects an address from the dropdown, populate the address
  22. // fields in the form.
  23. autocomplete.addListener('place_changed', fillInAddress);
  24. }
  25.  
  26. function fillInAddress() {
  27. // Get the place details from the autocomplete object.
  28. var place = autocomplete.getPlace();
  29.  
  30. for (var component in componentForm) {
  31. document.getElementById(component).value = '';
  32. document.getElementById(component).disabled = false;
  33. }
  34.  
  35. // Get each component of the address from the place details
  36. // and fill the corresponding field on the form.
  37. for (var i = 0; i < place.address_components.length; i++) {
  38. var addressType = place.address_components[i].types[0];
  39. if (componentForm[addressType]) {
  40. var val = place.address_components[i][componentForm[addressType]];
  41. document.getElementById(addressType).value = val;
  42. }
  43. }
  44. }
  45.  
  46. // Bias the autocomplete object to the user's geographical location,
  47. // as supplied by the browser's 'navigator.geolocation' object.
  48. function geolocate() {
  49. if (navigator.geolocation) {
  50. navigator.geolocation.getCurrentPosition(function(position) {
  51. var geolocation = {
  52. lat: position.coords.latitude,
  53. lng: position.coords.longitude
  54. };
  55. var circle = new google.maps.Circle({
  56. center: geolocation,
  57. radius: position.coords.accuracy
  58. });
  59. autocomplete.setBounds(circle.getBounds());
  60. });
  61. }
  62. }
  63.  
  64. https://code.google.com/p/geo-autocomplete/
  65.  
  66. http://geo-autocomplete.googlecode.com/svn/trunk/demo/ui.demo.html
  67.  
  68. if (1 < query.length) { // if 2 chars or more entered
  69. if (undefined == self.googleAutocomplete) { //... and Google Autocomplete not set yet
  70. console.debug('trigger autocomplete !');
  71. self.googleAutocomplete = new google.maps.places.Autocomplete(self.HtmlElement, self.settings.google_options);
  72. }
  73. // ! Beware : with 2 chars => we are only SETTING the autocompletion : actual suggestions will start from 3rd char...
  74. google.maps.event.addListener(self.googleAutocomplete, 'place_changed', function () {
  75. // do whatever with suggestion selected...
  76. }
  77. } else if (undefined != self.googleAutocomplete) { // if less than 2 chars
  78. console.debug('unbind autocomplete !');
  79. // removing autocompletion : Thx to Zme...@gmail.com
  80. //... on https://code.google.com/p/gmaps-api-issues/issues/detail?id=3429
  81. $(".pac-container").remove();
  82. google.maps.event.clearListeners(self.HtmlElement);
  83. self.googleAutocomplete = undefined;
  84. }
  85.  
  86. var customAuotInit = function(){
  87. var a = $('#address').val().length;
  88. if(a >= 1){
  89. initAutocomplete();
  90. }
  91. }
  92.  
  93. function initAutocomplete() {
  94. autocomplete = new google.maps.places.Autocomplete(
  95. {types: ['address']});
  96. autocomplete.addListener('place_changed', fillInAddress);
  97. }
Add Comment
Please, Sign In to add comment