Cannot locate user error when using html5 Geolocation
This is my code to get current location using Geolocation property of 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: 10开发者_运维知识库000, 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>
I am getting output: Cannot locate user
What is the solution for this? I have putted "html5_init.js" in my root directory of archive.
The correct validation is:
if (navigator.geolocation !== null)
精彩评论