使用html5 Geolocation时无法找到用户错误

时间:2011-08-10 07:49:21

标签: html5 geolocation blackberry-playbook

这是我使用HTML5的Geolocation属性获取当前位置的代码,

<!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 个答案:

答案 0 :(得分:1)

正确的验证是:

if (navigator.geolocation !== null)