0

I have a simple question that I can't seem to get around by. I am using Google Maps javascript to show specific locations this all works well when the user accepts Geolocation services. To test Google Maps considerIp is working, allowing google maps to use the public IP address also works fine. However, when I try to do the below, the showLocationBasedOnIP() method is not even being called.

function initMap(){
   if (navigator.geolocation) {
     navigator.geolocation.getCurrentPosition(showPosition);
   }
   else{
     showLocationsBasedOnIP();
   }
}

I have tested the showLocationsBasedOnIP independently by removing the geolocation checker, but when I try to integrate it as shown above, the method is not even being triggered.

What I want to achieve is that when the user denies geolocation, my showLocationsBasedOnIP() method should be triggered, in which I have a message to display on the browser that public IP address is being used and that location services will not be accurate unless the user turns on geolocation.

Is there something am missing?

evan
  • 5,443
  • 2
  • 11
  • 20
usr41059
  • 61
  • 4
  • Does `navigator.geolocation` exist when the user declines geolocation? – geocodezip May 19 '20 at 12:59
  • @geocodezip it doesn't. Google maps doesn't load anything, and yes I know that's because user denied services, but I want the other method in the else loop triggered – usr41059 May 19 '20 at 13:18
  • Why do you say `navigator.geolocation` doesn't exist? ([fiddle](https://jsfiddle.net/geocodezip/98Lyxdwr/2/) that logs it) It exists if I decline geolocation. Therefore your code will never execute the `else` path. – geocodezip May 19 '20 at 13:37
  • related question: [function fail never called if user declines to share geolocation in firefox](https://stackoverflow.com/questions/5947637/function-fail-never-called-if-user-declines-to-share-geolocation-in-firefox) – geocodezip May 19 '20 at 13:53
  • @geocodezip I am aware and thats exactly what am saying as well. What i want is for the else loop to execute the function because at that time the user has declined geolocation. – usr41059 May 19 '20 at 14:10
  • Since `(navigator.geolocation)` isn't false in that case, the `else` won't run. See the related question. – geocodezip May 19 '20 at 14:17

1 Answers1

0

You need to set your showLocationsBasedOnIP function as the error callback function for getCurrentPosition(). Like this:

<!DOCTYPE html>
<html>

  <body>
    <button onclick="getLocation()">Try It</button>
    <script>
      function getLocation() {
        if (navigator.geolocation) {
          navigator.geolocation.getCurrentPosition(showPosition, showLocationsBasedOnIP);
        } else {
          showLocationsBasedOnIP();
        }
      }

      function showPosition(position) {
        console.log("showing position: ", position.coords.latitude, position.coords.longitude);
      }

      function showLocationsBasedOnIP() {
        console.log("showing location based on IP");
      }

    </script>
  </body>

</html>

Now your function will execute in both cases; when the user doesn't allow location permissions and when the user's browser doesn't support geolocation.

Dharman
  • 30,962
  • 25
  • 85
  • 135
evan
  • 5,443
  • 2
  • 11
  • 20