navigator.geolocation.getCurrentPosition sometimes works sometimes doesn't

前端 未结 25 1629
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 13:33

So I have a pretty simple bit of JS using the navigator.geolocation.getCurrentPosition jammy.

$(document).ready(function(){
  $(\"#business-locate, #people-l         


        
相关标签:
25条回答
  • 2020-11-22 14:28

    For anyone working on an iPhone app...

    If your code is running in a UIWebView on iOS 9+, then you must set NSLocationWhenInUseUsageDescription within your app's plist.

    If you don't set it then getCurrentPosition will never call back and the user will never be prompted.

    0 讨论(0)
  • 2020-11-22 14:30

    Thanks to everyone for their input, this helped me.

    In addition to having to use watchPosition() instead of getCurrentPosition(), I also found that I needed to move the call from within document.ready() to the head.

    0 讨论(0)
  • 2020-11-22 14:31

    I noticed this problem recently myself, and I'm not sure how it comes about but it would appear sometimes firefox gets stuck on something loaded in cache. After clearing cache and restarting firefox it appears to function again.

    0 讨论(0)
  • 2020-11-22 14:32

    This works for me every time:

    navigator.geolocation.getCurrentPosition(getCoor, errorCoor, {maximumAge:60000, timeout:5000, enableHighAccuracy:true});
    

    Though it isn't very accurate. The funny thing is that on the same device if I run this it puts me off about 100 meters (every time), but if I go to google's maps it finds my location exactly. So although I think the enableHighAccuracy: true helps it to work consistently, it doesn't seem to make it any more accurate...

    0 讨论(0)
  • 2020-11-22 14:33

    I found, that this way doesn't work

    navigator.geolocation.getCurrentPosition(function() {...}, function(err) {...}, {});
    

    But

    this way works perfect

    function storeCoordinates(position) {
        console.log(position.coords.latitude, position.coords.longitude);
    }    
    
    function errorHandler() {...}
    
    navigator.geolocation.getCurrentPosition(storeCoordinates, errorHandler, { enableHighAccuracy: true, timeout: 20000, maximumAge: 0 });
    
    0 讨论(0)
  • 2020-11-22 14:34

    Same here people, this works perfect btw in Chrome (stable, dev and canary) just not in FF and Safari. It also works perfect on my iPhone and iPad (Safari!). This might be due to the relative newness of this feature (i.e. it is a bug). I spend almost a week on this now and I just cannot get it to work on those browsers

    Here's what I found:

    The first time you call getCurrentPosition it works perfect. Any subsequent call never returns, i.e. it does not fire the successCallback or the errorCallback functions. I added a few position options to my call to prove my point:

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

    and it times out every time (after the first successful call). I thought I could fix it with maximumAge, but that doesn't seem to be working as it is suppose to work either:

    navigator.geolocation.getCurrentPosition(successCallback, errorCallback,  {maximumAge:60000, timeout: 2000});
    

    this should prevent actually calling the getCurrentPosition function if you call it within 60 seconds, but it ignores this (however, this could be due because I actually refresh my page to trigger the second call, not sure if this is persistent accross calls)

    btw, even google's examples fail on these browsers which leads me to believe that this are indeed browser bugs, try it, load it twice in Safari and it won't work the second time.

    If anybody finds a solution for this, PLEASE let me know :-)

    Cheers.

    0 讨论(0)
提交回复
热议问题