Search code examples
javascriptangulargeolocation

Close geolocation prompt after timeout


Is there any way by which I can automatically close geolocation prompt (say after 5 seconds)?

Below is my function:

setCurrentPosition() {


const geoOptions = {
  enableHighAccuracy: false,
  timeout: 2000, // Wait 2 seconds
  maximumAge: 2000   //  Valid for 2 seconds
};

if ('geolocation' in navigator) {
  navigator.geolocation.getCurrentPosition((position) => {
    this.findAddressByCoordinates(position.coords.latitude, position.coords.longitude);
  }, (positionError => {
    console.log('error');
    console.log(positionError);
  }), geoOptions );


  if (this.currentMapData === undefined) {
    this.isComponentsValid.emit(false);
  } else {
    this.isComponentsValid.emit(true);
  }

}}

Solution

  • No, Once the chrome's prompt is shown, you can not close it with code. User will either block or allow.

    One trick is to use soft prompt instead of hard prompt, which basically means that you would create your own similar looking popup so that you can control this popup according to you (hide this after 5 seconds). Once user agrees to share his location on your custom popup, you can show the real chrome popup.

    For more information read this awesome article on UxCollective - https://uxdesign.cc/ask-for-push-notification-opt-in-properly-you-have-only-one-chance-64d66fac618e