Get Geolocation Data

Get Geolocation Data
0

#1

Tell us what’s happening:
The task is passed. BUT: there wasn’t any coords on virtual phone screen and, if I try this code in browser separately, it shows “Uncaught ReferenceError: $ is not defined”. what I’m doing wrong?

Your code so far

<script>
  // Only change code below this line.
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      $("#data").html("latitude: " + position.coords.latitude + "<br>longitude: " + position.coords.longitude);
    });
  }
  
  
  // Only change code above this line.
</script>
<div id = "data">
  <h4>You are here:</h4>
  
</div>
<style>
  #data {
    color: red;
  }
</style>

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36.

Link to the challenge:


#2

It is normal that your task passed, because the code you typed in was correct.

However, if you want to get the coordinates and display them, you have to do it outside the challenge, for example in a Codepen.

If you try this code in the Codepen, keep in mind that the $ symbol comes from jQuery, and that you have to include this using a script tag:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

If you have done everything correctly, you will get a prompt, which looks like this in Chrome:

Location

Finally, you have to allow it and it will display your location.


#3

thank you very much for your help! it helped me!


#4

I’m glad I was able to help.

If you have any further questions, don’t hesitate to ask!