Guest User

Untitled

a guest
Jan 18th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. Using google api location to auto complete the address
  2. Am using google location search api
  3. Am using two text box to autocomplete location
  4. for example in need enter origin and destination
  5. i can able to autocomplete only one text box at a time
  6. for first text box i can autocomplete and its working. Unable to do same
  7.  
  8. <script>
  9.  
  10. var placeSearch, autocompleter;
  11. var componentForm = {
  12. street_number: 'short_name',
  13. route: 'long_name',
  14. locality: 'long_name',
  15. administrative_area_level_1: 'short_name',
  16. country: 'long_name',
  17. postal_code: 'short_name'
  18. };
  19.  
  20. function initAutocompletes() {
  21.  
  22. autocompleter = new google.maps.places.Autocomplete(
  23. (document.getElementById('autocompleter')),
  24. {types: ['geocode']});
  25.  
  26. autocompleter.addListener('place_changed', fillInAddress);
  27. }
  28.  
  29. function fillInAddress() {
  30.  
  31. var place = autocompleter.getPlace();
  32.  
  33. for (var component in componentForm) {
  34. document.getElementById(component).value = '';
  35. document.getElementById(component).disabled = false;
  36. }
  37.  
  38. for (var i = 0; i < place.address_components.length; i++) {
  39. var addressType = place.address_components[i].types[0];
  40. if (componentForm[addressType]) {
  41. var val = place.address_components[i][componentForm[addressType]];
  42. document.getElementById(addressType).value = val;
  43. }
  44. }
  45. }
  46.  
  47. function geolocater() {
  48. if (navigator.geolocation) {
  49. navigator.geolocation.getCurrentPosition(function(position) {
  50. var geolocation = {
  51. lat: position.coords.latitude,
  52. lng: position.coords.longitude
  53. };
  54. var circle = new google.maps.Circle({
  55. center: geolocation,
  56. radius: position.coords.accuracy
  57. });
  58. autocompleter.setBounds(circle.getBounds());
  59. });
  60. }
  61. }
  62. </script>
  63. <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB042hvI_n-AgJG2YpvxkUgYssoWZTdGT8&libraries=places&callback=initAutocomplete"async defer></script>
  64.  
  65. </body>
  66. </html>
Add Comment
Please, Sign In to add comment