Guest User

Untitled

a guest
Sep 9th, 2024
3,389
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Google Maps Place Autocomplete</title>
  7. <style>
  8. /* Set the size of the map */
  9. #map {
  10. height: 400px;
  11. width: 100%;
  12. }
  13. /* Style the input field */
  14. #pac-input {
  15. margin-top: 10px;
  16. width: 300px;
  17. padding: 5px;
  18. font-size: 14px;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <input id="pac-input" type="text" placeholder="Search for a place">
  24. <div id="map"></div>
  25.  
  26. <!-- Load the Google Maps JavaScript API with the Places library -->
  27.  
  28. <script async defer
  29. src="https://maps.gomaps.pro/maps/api/js?key=Your-Aoi-KEY-From-Gomaps&libraries=geometry,places&callback=initMap">
  30. </script>
  31. <!-- <script async defer
  32. src="./gogleplaces2.js">
  33. </script> -->
  34.  
  35. <script>
  36.  
  37. let map;
  38. let autocomplete;
  39.  
  40. function initMap() {
  41. // Create the map centered on a default location
  42. map = new google.maps.Map(document.getElementById('map'), {
  43. center: { lat: -33.8688, lng: 151.2195 }, // Default to Sydney, Australia
  44. zoom: 13
  45. });
  46.  
  47. const input = document.getElementById('pac-input');
  48.  
  49. // Create the autocomplete object and bind it to the input field
  50. autocomplete = new google.maps.places.Autocomplete(input);
  51. autocomplete.bindTo('bounds', map);
  52.  
  53. // Set up the event listener for when the user selects a place
  54. autocomplete.addListener('place_changed', () => {
  55. const place = autocomplete.getPlace();
  56. if (!place.geometry) {
  57. console.log("No details available for the input: '" + place.name + "'");
  58. return;
  59. }
  60.  
  61. if (place.geometry.viewport) {
  62. map.fitBounds(place.geometry.viewport);
  63. } else {
  64. map.setCenter(place.geometry.location);
  65. map.setZoom(17); // Zoom to 17 if the place has no viewport
  66. }
  67.  
  68. // Place a marker on the selected location
  69. new google.maps.Marker({
  70. position: place.geometry.location,
  71. map: map
  72. });
  73. });
  74. }
  75. </script>
  76. </body>
  77. </html>
  78.  
Advertisement
Comments
  • YimingSEO
    1 year
    # HTML 2.49 KB | 0 0
    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4.     <meta charset="UTF-8">
    5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6.     <title>Google Maps Place Autocomplete</title>
    7.     <style>
    8.         /* Set the size of the map */
    9.         #map {
    10.             height: 400px;
    11.             width: 100%;
    12.         }
    13.         /* Style the input field */
    14.         #pac-input {
    15.             margin-top: 10px;
    16.             width: 300px;
    17.             padding: 5px;
    18.             font-size: 14px;
    19.         }
    20.     </style>
    21. </head>
    22. <body>
    23.     <input id="pac-input" type="text" placeholder="Search for a place">
    24.     <div id="map"></div>
    25.  
    26.     <!-- Load the Google Maps JavaScript API with the Places library -->
    27.  
    28.      <script async defer
    29.        src="https://maps.gomaps.pro/maps/api/js?key=Your-Aoi-KEY-From-Gomaps&libraries=geometry,places&callback=initMap">
    30.     </script>
    31.     <!-- <script async defer
    32.    src="./gogleplaces2.js">
    33. </script> -->
    34.  
    35.     <script>
    36.  
    37.         let map;
    38.         let autocomplete;
    39.  
    40.         function initMap() {
    41.             // Create the map centered on a default location
    42.             map = new google.maps.Map(document.getElementById('map'), {
    43.                 center: { lat: -33.8688, lng: 151.2195 }, // Default to Sydney, Australia
    44.                 zoom: 13
    45.             });
    46.  
    47.             const input = document.getElementById('pac-input');
    48.  
    49.             // Create the autocomplete object and bind it to the input field
    50.             autocomplete = new google.maps.places.Autocomplete(input);
    51.             autocomplete.bindTo('bounds', map);
    52.  
    53.             // Set up the event listener for when the user selects a place
    54.             autocomplete.addListener('place_changed', () => {
    55.                 const place = autocomplete.getPlace();
    56.                 if (!place.geometry) {
    57.                     console.log("No details available for the input: '" + place.name + "'");
    58.                     return;
    59.                 }
    60.  
    61.                 if (place.geometry.viewport) {
    62.                     map.fitBounds(place.geometry.viewport);
    63.                 } else {
    64.                     map.setCenter(place.geometry.location);
    65.                     map.setZoom(17); // Zoom to 17 if the place has no viewport
    66.                 }
    67.  
    68.                 // Place a marker on the selected location
    69.                 new google.maps.Marker({
    70.                     position: place.geometry.location,
    71.                     map: map
    72.                 });
    73.             });
    74.         }
    75.     </script>
    76. </body>
    77. </html>
Add Comment
Please, Sign In to add comment