I made an array with two elements using the browser's built in navigator. gpsLocation[0]
is my latitude, and gpsLocation1 is my longitude. Immediately before my jQuery code, using log to check my gpsLocation, I confirm that both values exist. Yet, when I assign the first element of my array to the html of a paragraph element, it says undefined. I even checked the coords it gave me and they're shockingly accurate. I've searched for an answer and there's a million similarly phrased questions but none of them seem to be trying to do this.
Here is my code:
function getLocation() {
var gpsLocation = [];
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
gpsLocation.push(position.coords.latitude);
gpsLocation.push(position.coords.longitude);
});
}
return gpsLocation;
}
function getWeather() {
var gpsLocation = getLocation();
console.log(gpsLocation);//Looks good in the console.
$(function(){
$('#test-paragraph').html('Latitude: ' + gpsLocation[0]);
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="weather">
<button id="test-button" onclick="getWeather()">Test Button</button>
<p id="test-paragraph">This is a sample paragraph for learning jQuery</p>
</div>
The second function name might seem random but weather is ultimately going to be its purpose. Still struggling to understand jQuery and json.