Pregunta

I want to execute functions in order but i constantly get 1,3,4,2 in console.Because of that ltt and lott remains 0 in function getDistanceFromLatLonInKm.Any ideas?Thanks in advance.

var ltt=0;
var lott=0;

if (navigator.geolocation){
    navigator.geolocation.getCurrentPosition(ajmo);
    console.log('1');
}

function ajmo(position){
    console.log('2');
    window.ltt=position.coords.latitude;
    window.lott=position.coords.longitude;
    document.write(window.ltt);
}

console.log('3');
document.write(window.ltt);
document.write("kurac:" + getDistanceFromLatLonInKm(45.332497,14.436384));

function getDistanceFromLatLonInKm(lat1,lon1){
    console.log('4');   
  //second
  var R = 6371; // Radius of the earth in km
  var dLat = deg2rad(ltt-lat1);  // deg2rad below
  var dLon = deg2rad(lott-lon1); 
  var a = 
    Math.sin(dLat/2) * Math.sin(dLat/2) +
    Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(ltt)) * 
    Math.sin(dLon/2) * Math.sin(dLon/2)
    ; 
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
  var d = R * c; // Distance in km
  return d;
}

function deg2rad(deg) {
  return deg * (Math.PI/180)
}
¿Fue útil?

Solución

The following changes will ensure that all console messages are in order 1,2,3,4

var ltt=0;
var lott=0;

if (navigator.geolocation){
    console.log('1');
    navigator.geolocation.getCurrentPosition(ajmo);
}

function ajmo(position){
    console.log('2');
    window.ltt=position.coords.latitude;
    window.lott=position.coords.longitude;
    document.write(window.ltt);

   console.log('3');
   document.write(window.ltt);
   document.write("kurac:" + getDistanceFromLatLonInKm(45.332497,14.436384));
}


function getDistanceFromLatLonInKm(lat1,lon1){
    console.log('4');   
  //second
  var R = 6371; // Radius of the earth in km
  var dLat = deg2rad(ltt-lat1);  // deg2rad below
  var dLon = deg2rad(lott-lon1); 
  var a = 
    Math.sin(dLat/2) * Math.sin(dLat/2) +
    Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(ltt)) * 
    Math.sin(dLon/2) * Math.sin(dLon/2)
    ; 
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
  var d = R * c; // Distance in km
  return d;
}

function deg2rad(deg) {
  return deg * (Math.PI/180)
}

Otros consejos

The reason why they are being called out of order is because the method "geolocation.getCurrentPosition(success, error, options)" is asynchronous.

https://developer.mozilla.org/en-US/docs/WebAPI/Using_geolocation

The (function)object AJMO that you are giving to it is actually used to describe the callback method. The script hits the line and sends an asynchronous request to the function, BUT it isn't going to wait for a response. The execution proceeds forward even though it hasn't heard back yet. The remaining lines are pretty simple with very low overhead so they resolve much, much quicker than the getCurrentPosition method. '2' is executed at the end, but this is only because the method finally "called back" to the success function you created, "AJMO".

If you want to make sure that the code is executed in order you could try this instead.


var ltt=0;
var lott=0;

function getDistanceFromLatLonInKm(lat1,lon1){
    console.log('4');   
  //second
  var R = 6371; // Radius of the earth in km
  var dLat = deg2rad(ltt-lat1);  // deg2rad below
  var dLon = deg2rad(lott-lon1); 
  var a = 
    Math.sin(dLat/2) * Math.sin(dLat/2) +
    Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(ltt)) * 
    Math.sin(dLon/2) * Math.sin(dLon/2)
    ; 
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
  var d = R * c; // Distance in km
  return d;
}

function ajmo(position){
    console.log('2');
    window.ltt=position.coords.latitude;
    window.lott=position.coords.longitude;
    document.write(window.ltt);

    console.log('3');
    document.write("kurac:" + getDistanceFromLatLonInKm(45.332497,14.436384));
}

if (navigator.geolocation){
    navigator.geolocation.getCurrentPosition(ajmo);
    console.log('1');
}

This puts the calls to '2', '3', and '4' within the successful callback method while still exposing "getDistanceFromLatLonInKm" as a separate function if you need it for some other purpose.

You don't need to use global variables here. Simply include the call to getDistanceFromLatLonInKm within ajmo, and this will ensure that your code does things in the right order.

// call getLocation
getLocation();

function getLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(ajmo);
  }
}

function ajmo(position) {
  var lat = position.coords.latitude;
  var lng = position.coords.longitude;

  // don't use document.write - it's considered bad practice
  // this will get access to an element with id="out"
  var out = document.getElementById('out');

  // now pass lat and lng as new parameters into getDistanceFromLatLonInKm
  out.innerHTML = 'kurac:' + getDistanceFromLatLonInKm(45.332497, 14.436384, lat, lng);
}

// now use the lat/lng arguments instead of the global variables
function getDistanceFromLatLonInKm(lat_origin, lon_origin, lat_pos, lng_pos) {
  var R = 6371;
  var dLat = deg2rad(lat_pos - lat_origin);
  var dLon = deg2rad(lng_pos - lng_origin);
  var a = 
    Math.sin(dLat / 2) * Math.sin(dLat / 2) +
    Math.cos(deg2rad(lat_origin)) * Math.cos(deg2rad(lat_pos)) * 
    Math.sin(dLon / 2) * Math.sin(dLon / 2)
    ; 
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  var d = R * c;
  return d;
}

function deg2rad(deg) {
  return deg * (Math.PI/180)
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top