hhfa1990
5/30/2015 - 8:25 PM

Calcular la distancia en Km entre dos coordenadas dadas

Calcular la distancia en Km entre dos coordenadas dadas

<?php

    echo calcCrow(11.111111, -22.222222, 33.333333, -44.444444);

    //This function takes in latitude and longitude of two location and returns the distance between them as the crow flies (in km)
    function calcCrow($lat1, $lon1, $lat2, $lon2)
    {
      $R = 6371; // km
      $dLat = toRad($lat2-$lat1);
      $dLon = toRad($lon2-$lon1);
      $lat1 = toRad($lat1);
      $lat2 = toRad($lat2);

      $a = sin($dLat/2) * sin($dLat/2) + sin($dLon/2) * sin($dLon/2) * cos($lat1) * cos($lat2); 
      $c = 2 * atan2(sqrt($a), sqrt(1-$a)); 
      $d = $R * $c;
      return $d;
    }

    // Converts numeric degrees to radians
    function toRad($Value) 
    {
        return $Value * pi() / 180;
    }
alert(calcCrow(11.111111, -22.222222, 33.333333, -44.444444).toFixed(1));



    //This function takes in latitude and longitude of two location and returns the distance between them as the crow flies (in km)
    function calcCrow(lat1, lon1, lat2, lon2) 
    {
      var R = 6371; // km
      var dLat = toRad(lat2-lat1);
      var dLon = toRad(lon2-lon1);
      var lat1 = toRad(lat1);
      var lat2 = toRad(lat2);

      var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
        Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2); 
      var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
      var d = R * c;
      return d;
    }

    // Converts numeric degrees to radians
    function toRad(Value) 
    {
        return Value * Math.PI / 180;
    }