navigator.geolocation.getCurrentPosition funktioniert manchmal und manchmal nicht
P粉831310404
P粉831310404 2023-08-27 15:01:59
0
1
385
<p>所以我使用 navigator.geolocation.getCurrentPosition jammy 有一个非常简单的 JS.</p> <pre class="brush:php;toolbar:false;">$(document).ready(function(){ $("#business-locate, #people-locate").click(function() { navigator.geolocation.getCurrentPosition(foundLocation, noLocation); }); navigator.geolocation.getCurrentPosition(foundLocation, noLocation); Funktion FoundLocation(Position) { var lat = position.coords.latitude; var lon = position.coords.longitude; var userLocation = lat + ', ' + lon; $("#business-current-location, #people-current-location").remove(); $("#Near-Me") .watermark("Aktueller Standort") .after("<input type='hidden' name='business-current-location' id='business-current-location' value='"+userLocation+"' />"); $("#people-Near-Me") .watermark("Aktueller Standort") .after("<input type='hidden' name='people-current-location' id='people-current-location' value='"+userLocation+"' />"); } Funktion noLocation() { $("#Near-Me").watermark("Standort konnte nicht gefunden werden"); $("#people-Near-Me").watermark("Standort konnte nicht gefunden werden"); } })//end DocReady</pre> <p>示„当前位置“的字段中, 并且使用经纬度数据作为其值创建两个隐藏字段(它们在开始时就被删除,这样它们就不会每次都重复)。还有两个按钮具有与其关联的单击功能, 可以执行相同的操作. 不幸的是, 每隔三次左右, 它就会起作用. 这是什么问题???</p>
P粉831310404
P粉831310404

Antworte allen(1)
P粉775788723

我有部分答案,但可惜不是完整的答案。

首先,要意识到 getCurrentPosition 的默认超时无限(!)。这意味着如果 getCurrentPosition 挂在后端的某个位置,您的错误处理程序将永远不会被调用

为了确保超时,请将可选的第三个参数添加到对 getCurrentPosition 的调用中,例如,如果您希望用户在提示发生了什么情况之前等待不超过 10 秒,请使用: p>

navigator.geolocation.getCurrentPosition(successCallback,errorCallback,{timeout:10000});

其次,我在不同的环境下体验到了截然不同的可靠性。在家里,我会在一两秒内收到回电,尽管准确性很差。

然而,在工作中,我经历了相当奇怪的行为变化:地理定位在某些计算机上一直有效(当然,IE 除外),其他计算机只能在 chrome 和 safari 中工作,但不能在 Firefox 中工作(gecko 问题?),其他计算机只能工作一次,然后失败——这种模式每小时、每天都在变化。有时您有一台“幸运”的计算机,有时则没有。也许在满月时屠宰山羊会有帮助?

我无法理解这一点,但我怀疑后端基础设施比在各种推动此功能的热衷书籍和网站中宣传的更不平衡。我真的希望他们能更直接地说明这个功能有多么不稳定,以及如果您希望错误处理程序正常工作,超时设置有多重要

我今天一直在尝试向学生教授这些内容,并遇到了尴尬的情况,我自己的计算机(在投影仪和几个大屏幕上)无声地发生故障,而大约 80% 的学生正在获得一个几乎立即得到结果(使用完全相同的无线网络)。当我的学生也犯错别字和其他错误时,当我自己的电脑也出现故障时,解决这些问题非常困难。

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!