How to call function when user Allows or Denies access to "Physical Location"?
Asked Answered
A

2

10

My application is powered by jQuery mobile and uses geolocation.

After my application attempts to get the user's location, the (Chrome) browser prompts the user:

Example.com wants to track your physical location [allow] [deny]

My goal is:

  1. If the user clicks "Allow", function 1 is called (location is used by app).
  2. If the user clicks "Deny", function 2 is called (address form appears).

How can I bind a function to the event that occurs (if any) when the user clicks the "Allow" or "Deny" button?

Acidic answered 18/9, 2011 at 18:11 Comment(2)
You can probably not bind anything to those buttons, but what does the function you call return when you click "deny"?Gloaming
When "deny" is clicked, the user is transferred to a page where they should fill in their address. The address is then sent to a web service that returns the geolocation.Acidic
M
23

The getCurrentPosition function accepts two function arguments. Heck, the first is executed when you allow and the other when you deny!

Documentation

http://jsfiddle.net/pimvdb/QbRHg/

navigator.geolocation.getCurrentPosition(function(position) {
    alert('allow');
}, function() {
    alert('deny');
});
Musician answered 18/9, 2011 at 18:16 Comment(3)
Is there an event that fires when the user simply dismisses the dialog or selects 'not now' (in Firefox), rather than clicking 'deny'?Genovevagenre
This does not work exactly. I wish it did. I get a few seconds of nothing before the event, or the alert in this example, fires. So, the user is left with no feedback for a few seconds which is bad...Howdy
is there any event when one closes popup? no "allow", no "deny" anly popup is closed.Fourier
C
2

Link to Docs

function handlePermission() { navigator.permissions.query({name:'geolocation'}).then(function(result) {
if (result.state == 'granted') {
  report(result.state);
  geoBtn.style.display = 'none';
} else if (result.state == 'prompt') {
  report(result.state);
  geoBtn.style.display = 'none';
  navigator.geolocation.getCurrentPosition(revealPosition,positionDenied,geoSettings);
} else if (result.state == 'denied') {
  report(result.state);
  geoBtn.style.display = 'inline';
}
result.onchange = function() {
  report(result.state);}});}function report(state) {
 console.log('Permission ' + state);}

I hope this works.

Circumstantiality answered 21/5, 2018 at 10:36 Comment(2)
It would be useful to include link where that codes was taken from : Permissions APIBrightwork
@Brightwork link added.Circumstantiality

© 2022 - 2024 — McMap. All rights reserved.