Advertisement
contatowellington

Untitled

Oct 16th, 2017
846
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Geolocation</title>
  5. <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
  6. <meta charset="utf-8">
  7. <style>
  8. /* Always set the map height explicitly to define the size of the div
  9. * element that contains the map. */
  10. #map {
  11. height: 100%;
  12. }
  13. /* Optional: Makes the sample page fill the window. */
  14. html, body {
  15. height: 100%;
  16. margin: 0;
  17. padding: 0;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div id="map"></div>
  23. <script>
  24. // Note: This example requires that you consent to location sharing when
  25. // prompted by your browser. If you see the error "The Geolocation service
  26. // failed.", it means you probably did not give permission for the browser to
  27. // locate you.
  28.  
  29. function initMap() {
  30. var map = new google.maps.Map(document.getElementById('map'), {
  31. center: {lat: -34.397, lng: 150.644},
  32. zoom: 16
  33. });
  34. var infoWindow = new google.maps.InfoWindow({map: map});
  35.  
  36. // Try HTML5 geolocation.
  37. if (navigator.geolocation) {
  38. navigator.geolocation.getCurrentPosition(function(position) {
  39. var pos = {
  40. lat: position.coords.latitude,
  41. lng: position.coords.longitude
  42. };
  43.  
  44. infoWindow.setPosition(pos);
  45. infoWindow.setContent('Location found.');
  46. map.setCenter(pos);
  47. }, function() {
  48. handleLocationError(true, infoWindow, map.getCenter());
  49. });
  50. } else {
  51. // Browser doesn't support Geolocation
  52. handleLocationError(false, infoWindow, map.getCenter());
  53. }
  54. }
  55.  
  56. function handleLocationError(browserHasGeolocation, infoWindow, pos) {
  57. infoWindow.setPosition(pos);
  58. infoWindow.setContent(browserHasGeolocation ?
  59. 'Error: The Geolocation service failed.' :
  60. 'Error: Your browser doesn\'t support geolocation.');
  61. }
  62. </script>
  63. <script async defer
  64. src="https://maps.googleapis.com/maps/api/js?key=AIzaSyC3rcDVwf8dmbKwtr2V0haRhDjAseaSW3Q&callback=initMap">
  65. </script>
  66. </body>
  67. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement