lbg232 lbg232 - 9 days ago 6
Ajax Question

Problems with API

I'm trying to build a local weather app with APIs.
Basically my code ask for latitud and lon to google, to then give it to another party to figure out what city is, to give it to make the final reques for the weather.

•I have the lat and lon

•I have the link for the request for witch city is

•I have the weather for each city

How do I put the city inside the wheater url to make the final request for the weather?

I'm sorry is very messy but I don't understand much about APIs nor how to work with javascript with html. If you know a source that can help me I will apreciate it.
Thanks.

Here is the code if you want to see it from the project.
http://codepen.io/Lbg232/pen/YpVgyJ?editors=1011

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script>

//I don't really know what is this for.
var options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
};


function success(pos) {
var crd = pos.coords;

console.log('Latitude : ' + crd.latitude); //It does print it (it works)
console.log('Longitude: ' + crd.longitude);

//This is the link ready to ask for the JSON
var finUrl = "http://api.wunderground.com/api/b2383764d2fa3c12/geolookup/q/" + crd.latitude + "," + crd.longitude + ".json";

console.log(finUrl); //it prints the link

};


function error(err) {
console.warn('ERROR(' + err.code + '): ' + err.message);
};

navigator.geolocation.getCurrentPosition(success, error, options);


//The request for the weather in a spacific city.
jQuery(document).ready(function($) {
$.ajax({

//I wanted to put the specific city and country.
url : "http://api.wunderground.com/api/b2383764d2fa3c12/geolookup/conditions/q/CL/Santiago.json",

dataType : "jsonp",
success : function(parsed_json) {
var location = parsed_json['location']['city'];
var temp_f = parsed_json['current_observation']['temp_c'];
alert("Current temperature in " + location + " is: " + temp_f);
}
});
});
</script>


I know is not a very specific question and that is not very apropiate for stack overflow but I can't find the answer. Thanks.

Answer

You want to replace CL and Santiago of this url (http://api.wunderground.com/api/b2383764d2fa3c12/geolookup/conditions/q/CL/Santiago.json) by any other city and country? If yes, you can just create another function that return the city and country name like this:

function getCurrentCityAndCountry(){
var cityAndCountry;
var result = "http://api.wunderground.com/api/b2383764d2fa3c12/geolookup/q/" + crd.latitude + "," + crd.longitude + ".json";
if(result){
    cityAndCountry = {
        city: location.city,
        country:location.country_name
    }
}
return cityAndCountry;
};

Now, before you call the last API, call the function created above to get the city and country name, and put them in the url

var cityAndCountry = getCurrentCityAndCountry();
url : "http://api.wunderground.com/api/b2383764d2fa3c12/geolookup/conditions/q/"+cityAndCountry.city+"/"+cityAndCountry.country+".json"
Comments