通过网线连接时如何使用 navigator.geolocation 获取经纬度信息
如果我通过无线连接到互联网, navigator.geolocation.getCurrentPosition 返回大约纬度/经度信息。但是,当通过网线连接时,方法调用失败。这是设计使然还是我错过了什么?下面是示例 html 页面,我使用 Chrome 5 来测试它。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Geocode Test</title>
<script type="text/javascript">
function getGeoLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
alert(position);
},
function () {
alert("oops");
});
}
}
</script>
</head>
<body>
<h1>Get Current Location</h1>
<button id='btnGetLocation' onclick='getGeoLocation()'>Get Current Location</button>
</body>
</html>
TIA
navigator.geolocation.getCurrentPosition returns the approx lat/long information if I connect to the internet via Wireless. However the method call fails when connected via Network cable. Is this by design or am I missing something? Below is the sample html page and I am using Chrome 5 to test it.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Geocode Test</title>
<script type="text/javascript">
function getGeoLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
alert(position);
},
function () {
alert("oops");
});
}
}
</script>
</head>
<body>
<h1>Get Current Location</h1>
<button id='btnGetLocation' onclick='getGeoLocation()'>Get Current Location</button>
</body>
</html>
TIA
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Chrome 上的地理定位 API 实现更喜欢使用 WiFi 三角测量而不是 IP 地理定位(与 Tony Miller 的答案相反),尽管我相信 Google 定位服务在必要时确实会依靠 IP 地址,或者至少使用 IP 地址来确认其猜测。
当您通过网线连接后,WiFi 仍在运行吗?通常,这些服务不需要您连接到特定的 WiFi 网络,它们只是使用所有可见无线网络的完整列表。但如果您关闭了计算机上的 WiFi 或关闭了附近可用的无线网络,这将在很大程度上阻止 navigator.geolocation 工作。
The Geolocation API implementation on Chrome prefers to use WiFi triangulation rather than IP geolocation (contra Tony Miller's answer), though I believe Google Location Services does fall back on IP address when it has to, or at least uses the IP address to confirm its guess.
When you're connected via a network cable do you still have WiFi running? Commonly these services don't require that you be connected to a particular WiFi network, they just use a full list of all the visible wireless networks. But if you turned off WiFi on your computer or turned off the available wireless networks nearby, that would largely prevent navigator.geolocation from working.
我没有看到根据网络进行地理定位的能力有任何差异。有关获取地理位置数据的更全面的方法,请参阅 Google 地图地理位置代码示例。您应该做的是将无线IP 地址与有线IP 地址进行比较。也许正在进行一些 NAT 转换,或者无线 IP 值尚未放入您正在访问的 IP 到地理数据库中。
I've not seen any difference in the ability to Geolocate depending on network. For a more comprehensive method of getting geolocation data, see the Google Maps Geolocation Code Example. What you should do is compare the wireless IP address with the wired IP address. Perhaps there's some NAT translation going on, or alternatively the wireless IP value hasn't been put into the IP-to-geo database you're accessing.