简体   繁体   中英

Javascript GeoLocation is not working on Chrome

I'm trying to take the geolocation of the User and then do a query. In Mozilla Firefox it works fine also in Safari.... but in Chrome it doesnt work at all.

window.onload = function(){
  if(navigator.geolocation){

    navigator.geolocation.getCurrentPosition(function(position){

        var latitude = position.coords.latitude,
            longitude = position.coords.longitude;
      console.log(latitude + " " + longitude);

    },handleError);

    function handleError(error){
      //Handle Errors
     switch(error.code) {
        case error.PERMISSION_DENIED:
            console.log("User denied the request for Geolocation.");
            break;
        case error.POSITION_UNAVAILABLE:
            console.log("Location information is unavailable.");
            break;
        case error.TIMEOUT:
           console.log("The request to get user location timed out.");
            break;
        case error.UNKNOWN_ERROR:
           console.log("An unknown error occurred.");
            break;
    }
    }
  }else{
    // container.innerHTML = "Geolocation is not Supported for this browser/OS.";
    alert("Geolocation is not Supported for this browser/OS");
  }
};

And i get the error 1

User denied the request for Geolocation.

But i haven't denied any request, actually the popup window doesnt come up at all.

I've went to Chrome Settings > Show advanced > Privacy > Content Settings > Location allow for all.

Restarted chrome and nothing happened. I'm sure my code is 100% legit so does anyone know how to deal with it? Thanks!

Ok this was quite easy... Chrome since 20 of April 2016 has disabled the Geolocation API for insecure websites (without https)... link here

https://developers.google.com/web/updates/2016/04/geolocation-on-secure-contexts-only

So dont worry..

The easiest way is to click on the area left to the address bar and change location settings there. It allows to set location options even for file:/// and all other types

在此处输入图片说明

If you are using chrome, please have a look at the answer below:

HTML 5 Geo Location Prompt in Chrome

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM