-5

I would like to ask you to help me solve the following question which is giving me trouble: How do I isolate the latitude and longitude variables to display them outside the function

navigator.geolocation.getCurrentPosition(getLatLon);
function getLatLon(position) {
    var latitude = "";
    var longitude = "";
    latitude = position.coords.latitude;
    longitude = position.coords.longitude;
}
document.write("Latitude is "+latitude);
document.write("Longitude is "+longitude);
Michael M.
  • 10,486
  • 9
  • 18
  • 34
Gibson
  • 1
  • 1

1 Answers1

-1
var latitude;
var longitude;

navigator.geolocation.getCurrentPosition(getLatLon);
function getLatLon(position) {
  latitude = "";
  longitude = "";
 latitude = position.coords.latitude;
 longitude = position.coords.longitude;
}
document.write("Latitude is "+latitude);
document.write("Longitude is "+longitude)

declare them outside of the function or at the top of your script file(global variables)