lbg232 lbg232 - 1 year ago 135
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.

Here is the code if you want to see it from the project.

<script src=""></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 = "" + 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($) {

//I wanted to put the specific city and country.
url : "",

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);

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 Source

You want to replace CL and Santiago of this url ( 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 = "" + crd.latitude + "," + crd.longitude + ".json";
    cityAndCountry = {
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 : """/"".json"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download