Check if Geolocation was allowed and get Lat Lon

て烟熏妆下的殇ゞ 提交于 2019-12-18 19:49:12

问题


I'm building a small script that clients will install on their page. Geolocation is not necessary for it, however if it exists it would be nice to have. Is there a way for me to check if the clients page has requested geolocation information and if the user selected allow get the lat & lon without creating another prompt?


回答1:


It isn't possible with the geolocation API but it is possible with the new permission API

This code is useful if you want to try to receive the coordinates but don't want to bother the user with a prompt dialog cuz the coordinates is not that important and may as well fallback to geoIP

function getCoords() {
  return new Promise((resolve, reject) =>
    navigator.permissions ?

      // Permission API is implemented
      navigator.permissions.query({
        name: 'geolocation'
      }).then(permission =>
        // is geolocation granted?
        permission.state === "granted"
          ? navigator.geolocation.getCurrentPosition(pos => resolve(pos.coords)) 
          : resolve(null)
      ) :

    // Permission API was not implemented
    reject(new Error("Permission API is not supported"))
  )
}

getCoords().then(coords => console.log(coords))

This will resolve to null if permission hasn't been granted, cast a error if some javascript code wasn't supported by the browser and resolve to the coordinates if it has been granted




回答2:


You can detect if the feature exists by examining the window object.

if('geolocation' in window){
     navigator.geolocation.getCurrentPosition(...);
}

This will cause a single user prompt to come up asking the user if they wish to share their location with your page.

If geolocation is not available in the browser, this will not run at all.


EDIT If you've already prompted the user on this page load for allowing geolocation access, it will NOT prompt you again. If the user navigates away and back to this page, it will re-prompt them.

You may make subsequent calls to the API without prompting during that page session.

Reference: MDN Geolocation -- Watching the current position




回答3:


According to the API it is not possible to find out of the user has already permitted the current site to retrieve the user's location without causing a prompt if he hasn't. See Endless answer

I'd suggest you to allow users to pass an argument when embedding your script telling it if it should use geolocation features or not.




回答4:


I also encountered the same problem. And, after I search and experiment I finally found the answer.

You can add this JS code :

navigator.permissions.query({name:'geolocation'}).then(function(result) {
  // Will return ['granted', 'prompt', 'denied']
  console.log(result.state);
});

Then you can use your custom code as needed.

source : https://developer.mozilla.org/en-US/docs/Web/API/Navigator/permissions




回答5:


Whether or not you're prompted seems to depend on the browser and what 'privacy' settings it's got. Safari 6 has 3 settings: prompt once per site, prompt once per site per day, and deny. Chrome 23 has 3 settings: allow all, ask, and deny all. Sure would get good if there was a way to check if it's already allowed without bothering the user.



来源:https://stackoverflow.com/questions/10077606/check-if-geolocation-was-allowed-and-get-lat-lon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!