aakash2310

Untitled

Feb 15th, 2023
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Choose a Location on the Map</title>
  5. <style>
  6. #map {
  7. height: 400px;
  8. width: 100%;
  9. }
  10. </style>
  11. </head>
  12. <body>
  13. <h1>Choose a Location on the Map</h1>
  14. <div id="map"></div>
  15.  
  16. <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY"></script>
  17. <script>
  18. function initMap() {
  19. var map = new google.maps.Map(document.getElementById('map'), {
  20. center: {lat: 37.7749, lng: -122.4194}, // Set the default center of the map
  21. zoom: 12 // Set the default zoom level of the map
  22. });
  23.  
  24. var marker = new google.maps.Marker({
  25. map: map,
  26. position: {lat: 37.7749, lng: -122.4194}, // Set the default position of the marker
  27. draggable: true // Allow the marker to be dragged by the user
  28. });
  29.  
  30. // Add an event listener to update the marker position and form fields when the user clicks on the map
  31. map.addListener('click', function(event) {
  32. marker.setPosition(event.latLng);
  33. updateFormFields(marker.getPosition());
  34. });
  35.  
  36. // Add an event listener to update the latitude and longitude input fields and form fields when the marker is dragged
  37. marker.addListener('dragend', function() {
  38. updateFormFields(marker.getPosition());
  39. });
  40.  
  41. // Define a function to update the form fields with the selected location's information
  42. function updateFormFields(latLng) {
  43. var geocoder = new google.maps.Geocoder();
  44. geocoder.geocode({location: latLng}, function(results, status) {
  45. if (status === 'OK') {
  46. if (results[0]) {
  47. document.getElementById('address').value = results[0].formatted_address;
  48. document.getElementById('lat').value = latLng.lat();
  49. document.getElementById('lng').value = latLng.lng();
  50. document.getElementById('place_id').value = results[0].place_id;
  51. document.getElementById('types').value = results[0].types.join(", ");
  52. } else {
  53. window.alert('No results found');
  54. }
  55. } else {
  56. window.alert('Geocoder failed due to: ' + status);
  57. }
  58. });
  59. }
  60. }
  61. </script>
  62.  
  63. <script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCdunhIl23I5p-mPqB0Zkokgz6IxTXx3Rs&callback=initMap"></script>
  64.  
  65. <form>
  66. <label for="address">Address:</label>
  67. <input type="text" id="address" name="address" readonly>
  68. <br>
  69. <label for="lat">Latitude:</label>
  70. <input type="text" id="lat" name="lat" readonly>
  71. <br>
  72. <label for="lng">Longitude:</label>
  73. <input type="text" id="lng" name="lng" readonly>
  74. <br>
  75. <label for="place_id">Place ID:</label>
  76. <input type="text" id="place_id" name="place_id" readonly>
  77. <br>
  78. <label for="types">Types:</label>
  79. <input type="text" id="types" name="types" readonly>
  80. </form>
  81. </body>
  82. </html>
  83.  
Advertisement
Add Comment
Please, Sign In to add comment