2011-08-10 134 views
1

時,這是我的代碼使用HTML5的地理位置屬性來獲取當前位置無法找到用戶錯誤,使用HTML5地理位置

<!DOCTYPE HTML> 
<html> 
<head> 
    <script type="text/javascript" src="html5_init.js" id="html5_init"></script> 
    <script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script> 
    <script type="text/javascript"> 


    function TestGeo()  
    { 
     if (navigator.geolocation)  
     { 
       navigator.geolocation.getCurrentPosition(TestMap, error ,{maximumAge: 30000, timeout: 10000, enableHighAccuracy: true});  
      } 
     else  
     { 
       alert("Sorry, but it looks like your browser does not support geolocation."); 
     } 
    } 

    var map; 

    function TestMap(position) 
    { 
     //var latitude = position.coords.latitude; 
     //var longitude = position.coords.longitude; 
     //alert("Your coordinates are " + position.coords.latitude + ", " + position.coords.longitude); 
     var coords = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); 
     var mapOptions = 
      { 
         zoom: 10, 
         center: coords, 
         mapTypeControl: false, 
         navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL}, 
         mapTypeId: google.maps.MapTypeId.ROADMAP 
      }; 
     map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions); 
     var marker = new google.maps.Marker({ position: coords, map: map, title: "Your current location!" }); 

    } 

    function error() 
    { 
        alert("Cannot locate user"); 
    } 
    </script> 
</head> 
<body> 

<form name="myform"> 
    <input type="button" value="Geolocation" onclick="TestGeo()"> 
    <div id="map_canvas" style="width:500px; height:450px; border:1px solid #666666; "></div> 
</form> 

</body> 
</html> 

我得到的輸出:無法找到用戶

,這是什麼解決方案? 我把「html5_init.js」放在我的檔案根目錄下。

回答

1

正確的驗證是:

if (navigator.geolocation !== null)