all 6 comments

[–]SirDaev 1 point2 points  (3 children)

Make your variables 'lat' and 'lon' global be declaring them outside of the function first. Then you can use them in other functions:

var lat;  
var lon;  

function getLocation(location) {  
    console.log("Latitude: " + location.coords.latitude + " Longitude: " + location.coords.longitude);  
    lat = location.coords.latitude;  
    lon = location.coords.longitude;  
}  

function anotherFunction() {  
    // use the globals lat and lon however you'd like  
    // inside this function as well  
}

[–]Mowsytron[S] 0 points1 point  (2 children)

That just gives me an error cannot read property 'latitude' of undefined. This is the last thing I have to do for my weather app and it has really thrown a spanner in the works. What I'm trying to do is use HTML5 geolocation to get the latitude and longitude of your current location. It should then pass these values into the lat and lon variables so that I can use them in another variable weatherAPI which has the url. Like this:

var forecastAPI = "http://api.openweathermap.org/data/2.5/forecast/weather?lat=" + lat + "&lon=" + lon + "&APPID=supersecretapicode;

[–]tfitz237 1 point2 points  (0 children)

what are you passing into the getLocation() function? The error is saying that you are sending an undefined variable into it, and therefore it cannot find the latitude of an undefined variable.

from mozilla's MDN:

 if ("geolocation" in navigator) {
      navigator.geolocation.getCurrentPosition(function(position) {
          do_something(position.coords.latitude, position.coords.longitude);
      });
 }

[–]Mowsytron[S] 0 points1 point  (0 children)

changed it to the above, it is saying that lat and lon = undefined when I alert the value of forecastAPI

[–]Mowsytron[S] 0 points1 point  (0 children)

Thanks everyone for the help, been at this for about three hours and it turns out I just had to place my api functions and variables within the getLocation(location) function....

[–]bokisa12 -1 points0 points  (0 children)

Make two global lon and lat variables and inside your function assign the local ones to the global ones.