2013-07-04 86 views
0

所以我试图在Javascript中首次使用地理定位。我基本上复制了来自另一个堆栈溢出jsfiddle示例的代码,这工作,但我得到一些奇怪的错误。有一段时间,我在JS控制台中得到了“最大的callstack大小超过jquery”错误(并且像调用getLocation()的20个调用),现在我没有收到任何回应 - 除了地址栏中带有X的小目标图标它表示位置权限被拒绝(即使在自动允许所有位置请求后)。这是在铬。试过safari,它发出了一个警告,要求许可,但之后什么都没有发生。仍然没有错误。有什么建议么?找不到地理位置

$(window).load(function() { 
    function getLocation() { 
     if (navigator.geolocation) { 
      navigator.geolocation.getCurrentPosition(showPosition); 
     } 
     else { 
      $('#test').write("Couldn't fetch location."); 
     }; 
    }; 

    function showPosition(position) { 
     $('#test').write("<h1>Latitude: \"" + position.coords.latitude + "\"</h1>"); 
     $('#test').write("<h1>Longitude: \"" + position.coords.longitude + "\"</h1>"); 
    }; 

    getLocation(); 
});, 

回答

0

有jQuery中没有write()方法,并在结尾有有一个逗号。
修复语法错误,它应该工作:

$(function() { 
    function getLocation() { 
     if (navigator.geolocation) { 
      navigator.geolocation.getCurrentPosition(showPosition); 
     } 
     else { 
      $('#test').html("Couldn't fetch location."); 
     } 
    } 

    function showPosition(position) { 
     $('#test').html("<h1>Latitude: \"" + position.coords.latitude + "\"</h1>"); 
     $('#test').append("<h1>Longitude: \"" + position.coords.longitude + "\"</h1>"); 
    } 

    getLocation(); 
}); 

FIDDLE

+0

谢谢!逗号是在这里粘贴代码的错字,但显然问题在于“写入”命令。尽管Chrome浏览器仍然在Safari和Firefox中运行,但仍然没有获得任何结果 - 有什么想法? – wkd

+0

@wkd - 它可以在Chrome中使用? – adeneo

+0

https://code.google.com/p/chromium/issues/detail?id=100353看起来像是本地托管文件的铬版本问题。 – wkd

0
<script> 

var x=document.getElementById("test"); 

function getLocation() 
{ 
if(navigator.geolocation{navigator.geolocation.getCurrentPosition(showPosition); 
} 
else { x.innerHTML = "Geolocation is not supported by this browser."; 
}}function showPosition(position){x.innerHTML="Latitude: " + position.coords.latitude + <br>Longitude: " + position.coords.longitude; } 

</script>