1

I am building a website that asks permission to use a person's location using - navigator.geolocation.getCurrentPosition(positionFound).

Using javascript or jQuery, how can I detect if a user denies permission, the moment after they deny it?

My code looks something like this -

function Geolocation() {
    navigator.geolocation.getCurrentPosition(positionFound)
}

function positionFound(position, found) {
    if (position.coords) {
    //do stuff
    }
}

Part of the problem is, the code after navigator is reached before the user accepts or denies permission to use the computer's location.

A Bogus
  • 3,852
  • 11
  • 39
  • 58
  • 1
    Read some docs: https://developer.mozilla.org/en-US/docs/Web/API/Geolocation.getCurrentPosition?redirectlocale=en-US&redirectslug=Web%2FAPI%2Fwindow.navigator.geolocation.getCurrentPosition - the second parameter to `getCurrentPosition` is for a function to be called for errors, receiving an error object with details ( https://developer.mozilla.org/en-US/docs/Web/API/PositionError ) – Ian Oct 15 '13 at 13:28
  • possible duplicate of [How to call function when user Allows or Denies access to "Physical Location"?](http://stackoverflow.com/questions/7463367/how-to-call-function-when-user-allows-or-denies-access-to-physical-location) – Blazemonger Oct 15 '13 at 13:40

2 Answers2

4

navigator.geolocation.getCurrentPostions can take two parameters success, error

Change your code to:

function positionFound(position) {
    // dostuff
}

function positionNotFound(error) {
    // Handle error
}

function Geolocation() {
    navigator.geolocation.getCurrentPosition(positionFound, positionNotFound)
}

Source

ediblecode
  • 11,701
  • 19
  • 68
  • 116
0

Ian is right, look at this example using getCurrentPosition