I'm trying to make a function that takes in a users location and then loops through a JSON file of station locations to determine which is the closest station. The issue I am having is with how to include both the location object and the JSON file as arguments in the function.
I am getting the location by using:
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(findNearestStation);
} else {
return "James Street";
}
}
I then want to use the function findNearestStation to take in a JSON as an argument and use the location passed by getLocation to find the nearest station. Something like this:
function findNearestStation(position, json) {
var UserLat = position.coords.latitude;
var UserLong = position.coords.longitude;
for (var i = 0; i < json.stations.length; i++) {
compare and find the min distance...
}
}
Any help would be hugely appreciated. Thanks.
Try an anonymous function:
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(pos) {
findNearestStation(pos, json);
});
} else {
return "James Street";
}
}