2016-10-11 48 views
0
<!DOCTYPE html> 
<html> 
    <head> 
    <title>Geolocation</title> 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> 
    <meta charset="utf-8"> 
    <style> 
     html, body { 
     height: 100%; 
     margin: 0; 
     padding: 0; 
     } 
     #map { 
     height: 100%; 
     } 
    </style> 
    </head> 
    <body> 
    <div id="map"></div> 
    <script> 
     // Note: This example requires that you consent to location sharing when 
     // prompted by your browser. If you see the error "The Geolocation service 
     // failed.", it means you probably did not give permission for the browser to 
     // locate you. 

     function initMap() { 
     var map = new google.maps.Map(document.getElementById('map'), { 
      center: {lat: -34.397, lng: 150.644}, 
      zoom: 6 
     }); 
     var infoWindow = new google.maps.InfoWindow({map: map}); 

     // Try HTML5 geolocation. 
     if (navigator.geolocation) { 
      navigator.geolocation.getCurrentPosition(function(position) { 
      var pos = { 
       lat: position.coords.latitude, 
       lng: position.coords.longitude 
      }; 

      infoWindow.setPosition(pos); 
      infoWindow.setContent('Location found.'); 
      map.setCenter(pos); 
      }, function() { 
      handleLocationError(true, infoWindow, map.getCenter()); 
      }); 
     } else { 
      // Browser doesn't support Geolocation 
      handleLocationError(false, infoWindow, map.getCenter()); 
     } 
     } 

     function handleLocationError(browserHasGeolocation, infoWindow, pos) { 
     infoWindow.setPosition(pos); 
     infoWindow.setContent(browserHasGeolocation ? 
           'Error: The Geolocation service failed.' : 
           'Error: Your browser doesn\'t support geolocation.'); 
     } 
    </script> 
    <script async defer 
    src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap"> 
    </script> 
    </body> 
</html> 

當我試圖運行該頁面時,它顯示一條消息,該頁面未正確加載Google地圖。查看JavaScript。瀏覽器已經有權訪問我的位置。但我不知道是什麼問題?地理定位在瀏覽器中不起作用

+1

什麼是錯誤消息是什麼呢?你在哪個瀏覽器中遇到這個錯誤? 「請參閱JavaScript。」 - 沒有在您的JavaScript引用「沒有正確加載谷歌地圖」 - 那麼是什麼錯誤 –

+0

您的代碼工作正常 - https://jsfiddle.net/m1jwzc2b/ –

+0

是你本地服務器上的HTTPS或HTTP?谷歌只允許自2016年中以來的ssl連接。我也有這個問題,並不得不重構我的應用程序使用ssl – mtizziani

回答

相關問題