Using coordinates to get information about the Weather

  • 2
  • Question
  • Updated 1 year ago
Hello there!

I was just reading this forum to get some answers, and I came across this thread https://apicommunity.wunderground.com/weatherapi/topics/api-call-returning-limited-data-in-surrey-bc.... This is my problem, right now. Using "Conditions" (country/city) is returning very limited results.

What I want to know is that if we can use the same "Conditions", but with the lat/lon format. By using coordinates, I get the results that I want (example: http://api.wunderground.com/api/33c725c475ee1f3a/conditions/q/10.9838099,-74.853037,13.json)... but is it "official"? Will this work forever with no problems?

Thank you!
Photo of Paulo Torres

Paulo Torres

  • 1 Post
  • 0 Reply Likes

Posted 1 year ago

  • 2
Photo of Brian Minor

Brian Minor

  • 4 Posts
  • 0 Reply Likes
Hi Paulo, Yes, that format is official and will work.  At the bottom of this page you will see various code examples and one is for lat/lon.

https://www.wunderground.com/weather/api/d/docs?d=data/geolookup 

My question is how can we incorporate geolookup into that format.  I have been using this code sample to pull my weather, but would like to use geolookup to import my lat/lon.  Some of these lines are too big for the window so I apologize in advance for the formatting.  It's the jquery example on:

https://www.wunderground.com/weather/api/d/docs?d=resources/code-samples 

I know how to do the geolookup part, I just don't know how to take those results and get them into the url with ajax being involved.  I'm unable to reference any variables in that url.  Thanks!

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>; <script> jQuery(document).ready(function($) { 
$.ajax({ url : "http://api.wunderground.com/api/8330df092372797a/geolookup/conditions/q/IA/Cedar_Rapids.json";, dataType : "jsonp", success : function(parsed_json) { var location = parsed_json['location']['city']; var temp_f = parsed_json['current_observation']['temp_f']; alert("Current temperature in " + location + " is: " + temp_f); } }); }); </script>
Photo of Brian Minor

Brian Minor

  • 4 Posts
  • 0 Reply Likes
I was able to figure out my geolocation issue.  Here is some sample code.

getLocation();

function getLocation() {    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else { 
        alert("Geolocation is not supported by this browser.");
    }
}

function showPosition(position) {
    var geoLat = position.coords.latitude;
    var geoLong = position.coords.longitude;
    var key = "insert API key here";
    var ForecastURL = "http://api.wunderground.com/api/"; + key + "/forecast/q/" + geoLat + "," + geoLong + ".json";
    var WeatherURL = "http://api.wunderground.com/api/"; + key + "/conditions/q/" + geoLat + "," + geoLong + ".json";

    $.ajax({
        url : WeatherURL,
        dataType : "jsonp",
        success : function(parsed_json) {
        var temp_f = parsed_json['current_observation']['temp_f'];
        document.getElementById("currentTemp").innerHTML = temp_f;
        }
    });
    $.ajax({
        url : ForecastURL,
        dataType : "jsonp",
        success : function(parsed_json) {
        var fore_high = parsed_json['forecast']['simpleforecast']['forecastday'][0]['high']['fahrenheit'];
        var fore_low = parsed_json['forecast']['simpleforecast']['forecastday'][0]['low']['fahrenheit'];
        document.getElementById("high1").innerHTML = fore_high;
        document.getElementById("low1").innerHTML = fore_low;
        
        }
    });
}