navigator.geolocation.getCurrentPosition always fail in chrome and firefox

后端 未结 7 751
予麋鹿
予麋鹿 2020-12-16 02:04

I got strange behavior when I tried to test my \"navigator.geolocation.getCurrentPosition\" web page. Here is my testing result and code:

my code:

fu         


        
相关标签:
7条回答
  • 2020-12-16 02:17

    I simulated this problem and found that the success callback functions were only called when the html page was hosted on a web server and not when opened from a filesystem.

    To test I opened the file directly from my C: drive and it the callbacks didn't work and then hosted the file on Internet Information Services (IIS) and the callbacks did work.

    <html>
    <body onload="detectLocation()">
    <!-- This html must be hosted on a server for navigator.geolocation callbacks to work -->
    
    <div id="status"></div>
    
    <script type="text/javascript">
    function detectLocation()
    {
      log("detectLocation() starting");
      if (navigator.geolocation)
      {
        log("navigator.geolocation is supported");
        navigator.geolocation.getCurrentPosition(geocodePosition, onError, { timeout: 30000 });
        navigator.geolocation.watchPosition(watchGeocodePosition);
      }
      else
      {
        log("navigator.geolocation not supported");
      }
    }
    function geocodePosition(){
        log("geocodePosition() starting");
    }
    
    function watchGeocodePosition(){
        log("watchGeocodePosition() starting");
    }
    
    function onError(error){
        log("error " + error.code);
    }
    function log(msg){
        document.getElementById("status").innerHTML = new Date() + " :: " + msg + "<br/>" + document.getElementById("status").innerHTML;
    }
    </script>
    </body>
    </html>
    
    0 讨论(0)
  • 2020-12-16 02:22

    You need to be using https, not http.

    The Chrome reference for this is here - https://developers.google.com/web/updates/2016/04/geolocation-on-secure-contexts-only

    0 讨论(0)
  • 2020-12-16 02:24

    I had same issue and solution was to increase the timeout duration as mobile network are slower than wired network

     {timeout: 30000, enableHighAccuracy: true, maximumAge: 75000}
    

    along with enabling cellular positioning

    In the Device Settings turn on "Wifi and Cellular positioning" option.

    0 讨论(0)
  • 2020-12-16 02:25

    I also got this message:

    message: "Network location provider at 'https://www.googleapis.com/' : Returned error code 404.", code: 2

    I could solve it by switching on my wifi adapter

    0 讨论(0)
  • 2020-12-16 02:33

    I know this is old topic but recently I had this error also:

    message: "Network location provider at 'https://www.googleapis.com/' : Returned error code 404.", code: 2

    The fix is to get api key for google maps and use it in your code

    <script src='https://maps.googleapis.com/maps/api/jscallback=initMap &signed_in=true&key=YOUR-API-KEY' async defer></script>

    Here you can get API KEY: https://developers.google.com/maps/documentation/javascript/get-api-key#key

    0 讨论(0)
  • 2020-12-16 02:36

    I had the same issue. Chrome browser wan not returning a position on 30000 miliseconds timeout. Firefox was not returning a position too. I added the option enableHighAccuracy and set it to false but nothing changed(false is the default option). When i change it to true then geolocation started working!
    This is my final code,

    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(
                function(position) {
                    // Get current cordinates.
                    positionCords = {"lat": position.coords.latitude, "lng": position.coords.longitude};
                },
                function(error) {
                    // On error code..
                },
                {timeout: 30000, enableHighAccuracy: true, maximumAge: 75000}
        );
    }  
    
    0 讨论(0)
提交回复
热议问题