yudaduy

Geolocation + Marker Example

Sep 20th, 2021
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Get Current Location + Marker</title>
  5. <style>
  6. /* Always set the map height explicitly to define the size of the div
  7. * element that contains the map. */
  8. #map {
  9. height: 100%;
  10. }
  11.  
  12. /* Optional: Makes the sample page fill the window. */
  13. html,
  14. body {
  15. height: 100%;
  16. margin: 0;
  17. padding: 0;
  18. }
  19.  
  20. .custom-map-control-button {
  21. background-color: #fff;
  22. border: 0;
  23. border-radius: 2px;
  24. box-shadow: 0 1px 4px -1px rgba(0, 0, 0, 0.3);
  25. margin: 10px;
  26. padding: 0 0.5em;
  27. font: 400 18px Roboto, Arial, sans-serif;
  28. overflow: hidden;
  29. height: 40px;
  30. cursor: pointer;
  31. }
  32. .custom-map-control-button:hover {
  33. background: #ebebeb;
  34. }
  35. </style>
  36.  
  37. <!-- jsFiddle will insert css and js -->
  38. </head>
  39. <body>
  40. <div id="map"></div>
  41.  
  42. <!-- Async script executes immediately and must be after any DOM elements used in callback. -->
  43. <script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
  44. <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap&v=weekly&channel=2" async></script>
  45.  
  46. <script>
  47.  
  48. let map, infoWindow;
  49.  
  50. function initMap() {
  51. map = new google.maps.Map(document.getElementById("map"), {
  52. center: { lat: -34.397, lng: 150.644 },
  53. zoom: 12,
  54. });
  55. infoWindow = new google.maps.InfoWindow();
  56.  
  57. const locationButton = document.createElement("button");
  58.  
  59. locationButton.textContent = "Pan to Current Location";
  60. locationButton.classList.add("custom-map-control-button");
  61. map.controls[google.maps.ControlPosition.TOP_CENTER].push(locationButton);
  62. locationButton.addEventListener("click", () => {
  63. // Try HTML5 geolocation.
  64. if (navigator.geolocation) {
  65. navigator.geolocation.getCurrentPosition(
  66. (position) => {
  67. const pos = {
  68. lat: position.coords.latitude,
  69. lng: position.coords.longitude,
  70. };
  71.  
  72. var marker = new google.maps.Marker({map: map, position: pos, clickable: true});
  73.  
  74. map.setCenter(pos);
  75. },
  76. () => {
  77. handleLocationError(true, infoWindow, map.getCenter());
  78. }
  79. );
  80. } else {
  81. // Browser doesn't support Geolocation
  82. handleLocationError(false, infoWindow, map.getCenter());
  83. }
  84. });
  85. }
  86.  
  87. function handleLocationError(browserHasGeolocation, infoWindow, pos) {
  88. infoWindow.setPosition(pos);
  89. infoWindow.setContent(
  90. browserHasGeolocation
  91. ? "Error: The Geolocation service failed."
  92. : "Error: Your browser doesn't support geolocation."
  93. );
  94. infoWindow.open(map);
  95. }
  96.  
  97. </script>
  98. </body>
  99. </html>
Advertisement
Add Comment
Please, Sign In to add comment