Advertisement
Jenderal92

Untitled

Oct 11th, 2022
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Autocomplete search address form using google map and get data into form example </title>
  5. <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script>
  6. </head>
  7. <body>
  8. <input id="searchInput" class="input-controls" type="text" placeholder="Enter a location">
  9. <div class="map" id="map" style="width: 100%; height: 300px;"></div>
  10. <div class="form_area">
  11. <input type="text" name="location" id="location">
  12. <input type="text" name="lat" id="lat">
  13. <input type="text" name="lng" id="lng">
  14. </div>
  15. <script>
  16. /* script */
  17. function initialize() {
  18. var latlng = new google.maps.LatLng(28.5355161,77.39102649999995);
  19. var map = new google.maps.Map(document.getElementById('map'), {
  20. center: latlng,
  21. zoom: 13
  22. });
  23. var marker = new google.maps.Marker({
  24. map: map,
  25. position: latlng,
  26. draggable: true,
  27. anchorPoint: new google.maps.Point(0, -29)
  28. });
  29. var input = document.getElementById('searchInput');
  30. map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
  31. var geocoder = new google.maps.Geocoder();
  32. var autocomplete = new google.maps.places.Autocomplete(input);
  33. autocomplete.bindTo('bounds', map);
  34. var infowindow = new google.maps.InfoWindow();
  35. autocomplete.addListener('place_changed', function() {
  36. infowindow.close();
  37. marker.setVisible(false);
  38. var place = autocomplete.getPlace();
  39. if (!place.geometry) {
  40. window.alert("Autocomplete's returned place contains no geometry");
  41. return;
  42. }
  43.  
  44. // If the place has a geometry, then present it on a map.
  45. if (place.geometry.viewport) {
  46. map.fitBounds(place.geometry.viewport);
  47. } else {
  48. map.setCenter(place.geometry.location);
  49. map.setZoom(17);
  50. }
  51.  
  52. marker.setPosition(place.geometry.location);
  53. marker.setVisible(true);
  54.  
  55. bindDataToForm(place.formatted_address,place.geometry.location.lat(),place.geometry.location.lng());
  56. infowindow.setContent(place.formatted_address);
  57. infowindow.open(map, marker);
  58.  
  59. });
  60. // this function will work on marker move event into map
  61. google.maps.event.addListener(marker, 'dragend', function() {
  62. geocoder.geocode({'latLng': marker.getPosition()}, function(results, status) {
  63. if (status == google.maps.GeocoderStatus.OK) {
  64. if (results[0]) {
  65. bindDataToForm(results[0].formatted_address,marker.getPosition().lat(),marker.getPosition().lng());
  66. infowindow.setContent(results[0].formatted_address);
  67. infowindow.open(map, marker);
  68. }
  69. }
  70. });
  71. });
  72. }
  73. function bindDataToForm(address,lat,lng){
  74. document.getElementById('location').value = address;
  75. document.getElementById('lat').value = lat;
  76. document.getElementById('lng').value = lng;
  77. }
  78. google.maps.event.addDomListener(window, 'load', initialize);
  79. </script>
  80. </body>
  81. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement