yudaduy

Geolocation Example

Sep 20th, 2021 (edited)
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 2.83 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.   <head>
  4.     <title>Get Current Location</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.           infoWindow.setPosition(pos);
  73.           infoWindow.setContent("Location found.");
  74.           infoWindow.open(map);
  75.           map.setCenter(pos);
  76.         },
  77.         () => {
  78.           handleLocationError(true, infoWindow, map.getCenter());
  79.         }
  80.       );
  81.     } else {
  82.       // Browser doesn't support Geolocation
  83.       handleLocationError(false, infoWindow, map.getCenter());
  84.     }
  85.   });
  86. }
  87.  
  88. function handleLocationError(browserHasGeolocation, infoWindow, pos) {
  89.   infoWindow.setPosition(pos);
  90.   infoWindow.setContent(
  91.     browserHasGeolocation
  92.       ? "Error: The Geolocation service failed."
  93.       : "Error: Your browser doesn't support geolocation."
  94.   );
  95.   infoWindow.open(map);
  96. }
  97.  
  98.     </script>
  99.   </body>
  100. </html>
Add Comment
Please, Sign In to add comment