我是 Javascript 的初学者,我正在尝试使这个示例正常工作。
这是一个简单的想法。
给定用户的位置,如果纬度小于 50,它将发送一条简单消息。
发生的情况是 if 根本不起作用.. 为什么?我需要创建一个新函数还是将 if 放在其中一个函数中?
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function initGeolocation()
{
if( navigator.geolocation )
{
// Call getCurrentPosition with success and failure callbacks
navigator.geolocation.getCurrentPosition( success, fail );
}
else
{
alert("Sorry, your browser does not support geolocation services.");
}
}
function success(position)
{
document.getElementById('userLong').value = position.coords.longitude;
document.getElementById('userLat').value = position.coords.latitude
}
function fail()
{
// Could not obtain location
}
</script>
</head>
<body onLoad="initGeolocation();">
<FORM NAME="rd" METHOD="POST" ACTION="index.html">
User Longitude: <INPUT TYPE="text" NAME="Long" ID="userLong" VALUE=""><br>
User Latitude : <INPUT TYPE="text" NAME="Lat" ID="userLat" VALUE=""><br>
<script>
if (userLat < 50){
document.write(" Great")
}
else { document.write( "Not Great")}
</script>
</body>
</html>
最佳答案
- 永远不要为
userLat
JavaScript 变量赋值。 - 在运行地理位置代码之前测试
userLat
。
将您的测试放在成功函数中,并测试position.coords.latitude
关于javascript - 查看用户地理标记不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21583424/