I'm attempting to create a function that declares a variable, and then utilize that function within another function to access the variable.
Here is my current approach:
function fetchLatitude(){
navigator.geolocation.getCurrentPosition(function(lat){
var latCoord = lat.coords.latitude;
return latCoord;
});
}
function fetchLongitude(){
navigator.geolocation.getCurrentPosition(function(lon){
var longCoord = lon.coords.latitude;
return longCoord;
});
}
function findImage(){
var latitude = fetchLatitude(),
longitude = fetchLongitude();
console.log(latitude + ' ' + longitude);
}
findImage();
As you can gather from the code provided, I have implemented functions fetchLatitude()
and fetchLongitude()
to extract the latitude and longitude values. In the findImage()
function, I aim to assign the result of fetchLatitude()
to a variable. However, the use of return latCoord;
did not yield the desired outcome.
Is there a better way to achieve this goal? If so, could you please provide an example for clarification?
You can access a demonstration on jsbin here.
Feel free to ask for more information if needed. Your assistance in this matter would be greatly appreciated. Thank you!