1

I am creating a web app using geolocation. So far I have it set up and working so when the user visits they are prompted to allow location services, then are presented with an alert (Not permanent, just for testing purposes.)

I am using this:

navigator.geolocation.getCurrentPosition(foundLocation, noLocation, {enableHighAccuracy:true});

function foundLocation(position)
{
    var lat = position.coords.latitude;
    var long = position.coords.longitude;
    alert('We know you are here '+ lat +','+ long);
}
function noLocation()
{
    alert('Could not find location');
}

Then I have a variable outside this called "address" which is the URL for the API call:

address = "http://api.wunderground.com/api/geolookup/hourly/conditions/astronomy/alerts/forecast/q/[LOCATION].json"

My question is how can I get the lat and long out of the function and insert them into the URL? I have tried a few methods, but they all return "undefined" so I am obviously doing something wrong.

Any help is greatly appreciated!

Thank you.

2 Answers 2

2

You have to understand the scope of a javascript variable, please, read this post: What is the scope of variables in JavaScript?

var address = '';

function setLocation(position)
{
    var lat = position.coords.latitude;
    var long = position.coords.longitude;
    address = "http://api.wunderground.com/api/geolookup/hourly/conditions/astronomy/alerts/forecast/q/" + lat + "," + long + ".json";
}

Besides, there is a better approach to solve your problem. The easiest approach is to create a global object with a unique name, with your variables as properties of that object and methods to change the variables, like:

var geolocation = {};
geolocation.latitude = 0;
geolocation.longitude = 0;
geolocation.address = "";
geolocation.setLocation = function(position) {
    geolocation.latitude = position.coords.latitude;
    geolocation.longitude = position.coords.longitude;
    geolocation.address = "http://api.wunderground.com/api/geolookup/hourly/conditions/astronomy/alerts/forecast/q/" + geolocation.latitude + "," + geolocation.longitude + ".json";
};
geolocation.show = function() {
  alert(geolocation.latitude + " " geolocation.longitude + " " + geolocation.address);
};

And so on. Now, everywhere in your file if you use:

geolocation.setLocation(position);
geolocation.show();

It will show the new values from the global object.

UPDATE

Keep in mind that a variable or object in javascript will be global if there is no wrapper around it, like another function or object.

Sign up to request clarification or add additional context in comments.

2 Comments

This is excellent, and thank you for all your help and explanations. I'm still learning so I appreciate people like yourself that are willing to help.
Thank you! Don't forget to vote up the guy on the "javascript variable scope" post if it helps you! Good luck!
1

Can you not just update the address directly from the function like this?

navigator.geolocation.getCurrentPosition(foundLocation, noLocation, {enableHighAccuracy:true});
var address = "http://api.wunderground.com/api/geolookup/hourly/conditions/astronomy/alerts/forecast/q/[LOCATION].json"

function foundLocation(position)
{
    var lat = position.coords.latitude;
    var long = position.coords.longitude;
    alert('We know you are here '+ lat +','+ long);
    address = address.replace('[LOCATION]', lat + ',' + long);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.