Home  >  Q&A  >  body text

navigator.geolocation.getCurrentPosition sometimes works and sometimes doesn't

<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); function 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("Current Location") .after("<input type='hidden' name='business-current-location' id='business-current-location' value='" userLocation "' />"); $("#people-Near-Me") .watermark("Current Location") .after("<input type='hidden' name='people-current-location' id='people-current-location' value='" userLocation "' />"); } function noLocation() { $("#Near-Me").watermark("Could not find location"); $("#people-Near-Me").watermark("Could not find location"); } })//end DocReady</pre> <p>基本上,这里发生的事情是我们获取当前位置,如果获取了,两个“水印”将被放置在两个表示“当前位置”的字段中,并且使用经纬度数据作为其值创建两个隐藏字段(它们在开始时就被删除,这样它们就不会每次都重复)。还有两个按钮具有与其关联的单击功能,可以执行相同的操作。 不幸的是,每隔三次左右,它就会起作用。 这是什么问题???</p>
P粉831310404P粉831310404441 days ago426

reply all(1)I'll reply

  • P粉775788723

    P粉7757887232023-08-28 09:08:27

    I have a partial answer, but unfortunately not a complete answer.

    First, realize that the default timeout for getCurrentPosition is infinite(!). This means that if getCurrentPosition hangs somewhere on the backend, your error handler will never be called.

    To ensure timeouts, add an optional third parameter to the call to getCurrentPosition, for example, if you want the user to wait no more than 10 seconds before being prompted what happened, use:

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

    Secondly, I have experienced completely different reliability in different environments. At home, I would receive a call back within a second or two, although the accuracy was poor.

    However, at work I experience a rather strange change in behavior: geolocation works all the time on some computers (except IE, of course), others only works in chrome and safari, but not in Firefox works (gecko issue?), other computers only work once and then fail - this pattern changes hourly and daily. Sometimes you have a "lucky" computer, sometimes you don't. Maybe slaughtering the goat during the full moon would help?

    I can't understand this, but I suspect that the backend infrastructure is more unbalanced than is advertised in the various enthusiast books and websites pushing this feature. I really wish they'd been more direct about how unstable this feature is and how important the timeout setting is if you want your error handler to work properly.

    I've been trying to teach these to students today and ran into the embarrassing situation of my own computer (on a projector and several large screens) silently failing while about 80% of the students Getting an almost instant result (using the exact same wireless network). When my students also make typos and other mistakes, and when my own computer malfunctions, it's very difficult to fix these problems.

    reply
    0
  • Cancelreply