3.0 KiB
3.0 KiB
id, title, challengeType, forumTopicId
id | title | challengeType | forumTopicId |
---|---|---|---|
587d7faf367417b2b2512be8 | Get Geolocation Data to Find A User's GPS Coordinates | 6 | 18188 |
Description
if (navigator.geolocation){
navigator.geolocation.getCurrentPosition(function(position) {
document.getElementById('data').innerHTML="latitude: " + position.coords.latitude + "<br>longitude: " + position.coords.longitude;
});
}
First, it checks if the navigator.geolocation
object exists. If it does, the getCurrentPosition
method on that object is called, which initiates an asynchronous request for the user's position. If the request is successful, the callback function in the method runs. This function accesses the position
object's values for latitude and longitude using dot notation and updates the HTML.
Instructions
script
tags to check a user's current location and insert it into the HTML.
Tests
tests:
- text: Your code should use <code>navigator.geolocation</code> to access the user's current location.
testString: assert(code.match(/navigator\.geolocation\.getCurrentPosition/g));
- text: Your code should use <code>position.coords.latitude</code> to display the user's latitudinal location.
testString: assert(code.match(/position\.coords\.latitude/g));
- text: Your code should use <code>position.coords.longitude</code> to display the user's longitudinal location.
testString: assert(code.match(/position\.coords\.longitude/g));
- text: You should display the user's position within the <code>data</code> div element.
testString: assert(code.match(/document\.getElementById\(\s*?('|")data\1\s*?\)\.innerHTML/g));
Challenge Seed
<script>
// Add your code below this line
// Add your code above this line
</script>
<h4>You are here:</h4>
<div id="data">
</div>
Solution
<script>
// Add your code below this line
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
document.getElementById('data').innerHTML = "latitude: " + position.coords.latitude + "<br>longitude: " + position.coords.longitude;
});
}
// Add your code above this line
</script>
<h4>You are here:</h4>
<div id="data">
</div>
</section>