skip to Main Content

I want to create an alert / message that shows me the users geodata after the page has been loaded.

Here ist the script for the geolocation that I want to be
displayed in the alert.

<script>
function getLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition);
  } else { 
    x.innerHTML = "Geolocation is not supported by this browser.";
  }
}

function showPosition(position) {
  x.innerHTML = "Latitude: " + position.coords.latitude + 
  "<br>Longitude: " + position.coords.longitude;
}

alert("Your location is:" + getLocation());
</script>

2

Answers


  1. Chosen as BEST ANSWER

    function getLocation() {
      if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
      } else { 
        innerHTML = "Geolocation is not supported by this browser.";
      }
    }
    
    function showPosition(position) {
      const result = "Latitude: " + position.coords.latitude + 
      "<br>Longitude: " + position.coords.longitude;
    
      innerHTML = result;
      alert(result);
    }
    
    getLocation();


  2. Assuming the code executes without error (you haven’t specified)… This should be successfully alerting "Your location is:undefined":

    alert("Your location is:" + getLocation());
    

    Because getLocation doesn’t return anything. Since the information you’re looking for is asynchronous, it only exists in or after a given callback operation. That callback operation (the function where the values you want exist) is:

    function showPosition(position) {
      x.innerHTML = "Latitude: " + position.coords.latitude + 
      "<br>Longitude: " + position.coords.longitude;
    }
    

    So that’s where you’d add your alert. For example:

    function showPosition(position) {
      const result = "Latitude: " + position.coords.latitude + 
      "<br>Longitude: " + position.coords.longitude;
    
      x.innerHTML = result;
      alert(result);
    }
    

    Using your original example below…

    NOTES:

    1. I am not making this a runnable Stack Snippet because geolocation is disabled there. It appears to work on CodePen though.
    2. I also had to define x. Which I assume you’d defined elsewhere in your code. Otherwise what would you expect it to be and why? And surely the browser would be producing an error on the console?
    <script>
      const x = {}; // Whatever x is in your code isn't necessary in this example
    
      function getLocation() {
        if (navigator.geolocation) {
          navigator.geolocation.getCurrentPosition(showPosition);
        } else { 
          x.innerHTML = "Geolocation is not supported by this browser.";
        }
      }
    
      function showPosition(position) {
        const result = "Latitude: " + position.coords.latitude + 
        "<br>Longitude: " + position.coords.longitude;
    
        x.innerHTML = result;
        alert(result);
      }
    
      getLocation();
    </script>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search