Вопрос

This code is working fine but i want function getLocation to have 2 arguments which are passed to function getDistanceFromLatLonInKm inside function ajmo.I want to do it because in this example getDistanceFromLatLonInKm has 2 hardcoded arguments and I want variables.

Any ideas?

Thanks in advance.

 getLocation();

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

function ajmo(position) {
  lat = position.coords.latitude;
  lng = position.coords.longitude;
  alert('kurac:' + getDistanceFromLatLonInKm(45.332497, 14.436384, lat, lng));
}

function getDistanceFromLatLonInKm(lat_origin, lon_origin, lat_pos, lon_pos) {
  var R = 6371;
  var dLat = deg2rad(lat_pos - lat_origin);
  var dLon = deg2rad(lon_pos - lon_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)
}
Это было полезно?

Решение

You pass a closure to getCurrentPosition:

function getLocation(lat, lon) {
  document.write(lo);
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(p) {
      ajmo(p, lat, lon);
    });
  }
}

The anonymous function will capture the value of lat and lon, and pass them to ajmo.

ajmo will then look like this:

function ajmo(position, originalLat, originalLon) {
  lat = position.coords.latitude;
  lng = position.coords.longitude;
  alert('kurac:' + getDistanceFromLatLonInKm(originalLat, originalLon, lat, lng));
}

Другие советы

I would have them as an object with lat/lng keys:

// pass the object in to getLocation
getLocation({lat: 45.332497, lng: 14.436384});

function getLocation(origin) {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function (position) {

      // pass both the position and the origin to ajmo
      ajmo(origin, position);
    });
  }
}

function ajmo(origin, position) {

  // declare your lat/lng vars properly
  var lat = position.coords.latitude;
  var lng = position.coords.longitude;

  // pass in origin as the first parameter
  alert('kurac:' + getDistanceFromLatLonInKm(origin, lat, lng));
}

function getDistanceFromLatLonInKm(origin, lat_pos, lon_pos) {
  var R = 6371;

  // Then just use dot notation to access the origin object
  var dLat = deg2rad(lat_pos - origin.lat);
  var dLon = deg2rad(lon_pos - origin.lon);
  var a = 
    Math.sin(dLat / 2) * Math.sin(dLat / 2) +
    Math.cos(deg2rad(origin.lat)) * 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;
}

Can you just do this.

function getLocation(param1, param2) {
  document.write(lo);
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(ajmo(null, param1, param2));
  }
}

function ajmo(position, param1, param2) {
  lat = position.coords.latitude;
  lng = position.coords.longitude;
  alert('kurac:' + getDistanceFromLatLonInKm(param1, param2, lat, lng));
}

From the above example the position variable was NOT being passed in from this line of code navigator.geolocation.getCurrentPosition(ajmo);

Thats how i would add the parameters

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top