info1atual

Geolocalização Google Mapas

Sep 6th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     <script>
  2.         // Note: This example requires that you consent to location sharing when
  3.         // prompted by your browser. If you see the error "The Geolocation service
  4.         // failed.", it means you probably did not give permission for the browser to
  5.         // locate you.
  6.         jQuery(document).ready(function($) {
  7.            
  8.         });
  9.         function initMap() {
  10.           var map = new google.maps.Map(document.getElementById('map'), {
  11.             center: {lat: -34.397, lng: 150.644},
  12.             zoom: 6
  13.           });
  14.           var infoWindow = new google.maps.InfoWindow({map: map});
  15.  
  16.           // Try HTML5 geolocation.
  17.           if (navigator.geolocation) {
  18.             navigator.geolocation.getCurrentPosition(function(position) {
  19.               var pos = {
  20.                 lat: position.coords.latitude,
  21.                 lng: position.coords.longitude
  22.               };
  23.  
  24.               infoWindow.setPosition(pos);
  25.               infoWindow.setContent('Você está aqui!');
  26.               map.setCenter(pos);
  27.               console.log(pos.lat+","+pos.lng)
  28.             }, function() {
  29.               handleLocationError(true, infoWindow, map.getCenter());
  30.             });
  31.           } else {
  32.             // Browser doesn't support Geolocation
  33.             handleLocationError(false, infoWindow, map.getCenter());
  34.           }
  35.         }
  36.  
  37.         function handleLocationError(browserHasGeolocation, infoWindow, pos) {
  38.           infoWindow.setPosition(pos);
  39.           infoWindow.setContent(browserHasGeolocation ?
  40.                                 'Error: The Geolocation service failed.' :
  41.                                 'Error: Your browser doesn\'t support geolocation.');
  42.         }
  43.  
  44.     </script>
  45.     <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&signed_in=true&callback=initMap"
  46.         async defer></script>
Add Comment
Please, Sign In to add comment