I am trying to run a JavaScript/JQuery function that pulls the timezone field from a JSON request from the DarkSky API. Currently, my code works by getting the lat and long and passing them into the URL so that the JSON can be received from the DarkSky API.
Currently, I have verified that I am getting lat and long variables and also that the URL is correct and received JSON. However, I am unable to place the timezone field into the tag within my webpage.
Below is my source code:
HTML:
<p id="timezone">Summary</p>
<span class="weather-location" id="currentLocation">Current Location</span>
Javascript/JQuery:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
var currentLoc = document.getElementById("currentLocation");
var tz = document.getElementById("timezone");
var url = "";
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
currentLoc.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
var lat = position.coords.latitude;
var long = position.coords.longitude;
currentLoc.innerHTML = "Latitude: " + lat +
"<br>Longitude: " + long;
url = "https://api.darksky.net/[API_KEY]/" + lat + "," + long;
getTodaysWeather(url);
}
function getTodaysWeather(url) {
$(document).ready(function () {
$.getJSON(url, function (results) {
tz.innerHTML = results.timezone;
});
});
}
</script>
EDIT:
Below is a snippet of the JSON I have pulled:
{ "latitude": 39.9145429, "longitude": -86.02194999999999, "timezone": "America/Indiana/Indianapolis", "currently": { "time": 1534743029, "summary": "Clear", "icon": "clear-night", "nearestStormDistance": 8, "nearestStormBearing": 219, "precipIntensity": 0, "precipProbability": 0, "temperature": 70.61, "apparentTemperature": 71.17, "dewPoint": 64.44, "humidity": 0.81, "pressure": 1014.05, "windSpeed": 5.1, "windGust": 9.07, "windBearing": 100, "cloudCover": 0.12, "uvIndex": 0, "visibility": 10, "ozone": 295.59 }, "minutely": { "summary": "Clear for the hour.", "icon": "clear-night", "data": [ { "time": 1534743000, "precipIntensity": 0, "precipProbability": 0 },
...............