<p>Click the button to get your coordinates.</p>
<button onclick="getLocation()">Try It</button>
<p id="demo"></p>
<script>
const x = document.getElementById("demo");
function getLocation() {
try {
navigator.geolocation.getCurrentPosition(showPosition);
} catch {
x.innerHTML = err;
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
</script>
I'm new to the JavaScript Geolocation API. So far it is very useful. However, I am trying to write some JavaScript that will calculate a price depending on what hemisphere you are in. North or South. So far I am using DEMO to return and display the latitude and longitude. I'm having trouble passing the response or that variable to another function. The goal is to recieve the Latitude and perform a simple calculation.
response would be equal to demo if demo > 0.00 then return northernHemisphere if demo < 0.00 then return southernHemisphere
The next goal would be to perform a very simple calculation on northernHemisphere and southernHemisphere.
If you are in northernHemisphere I want to display text or variable that it will cost $10 in shipping. If you are in the southernHemisphere I just want to return text that shipping will cost $20.
I'm sure there are way better and real shipping cost calculators but that's not the goal here. My goal is to return either north or south and display text of $10 or $20. Not looking for more accurate or complicity, just trying to learn basics of javascript variables and functions.