Know the direction between points by coordinate latitude and longitude with php

Asked

Viewed 149 times

1

Next guys I have 3 cities that we call point (A) (B) (C) all with latitude and longitude the topology of gets more or less like this:

A = -3.3105, -60.1694

B = -3.3242, -60.6281

C = -3.5970, -61.3930

Directions (A) <-Boat Traveling-> (B) <-Boat Traveling-> (C)

Note: Between point A to C I have a boat traveling, and I have latitude and longitude "coordinated" every 2 minutes of that boat:

Now comes question I want to know, if there’s any way to know if it’s going from the point to what point like this:

(A) --> (B)

(C) --> (B) 

(B) --> (A)

I hope you understand I ask that if someone can help me I really appreciate !

  • 1

    What is the formula for this calculation ? Because to "separate the values" and see the difference, just use one explode, thus separating LA from LO.

1 answer

3


[Update 2]

Final script (can still be improved):

// Detalhes
$n_pontoA = "Iranduba"; // Nome do ponto A
$n_pontoB = "Manaus";   // Nome do ponto B

// Coordenadas
$c1_barco = '-3.3105, -60.1694'; // Posição anterior do barco
$c2_barco = '-3.3137, -60.3477'; // Posição atual do barco
$c_pontoA = '-3.3242, -60.6281'; // Ponto A
$c_pontoB = '-3.5970, -61.3930'; // Ponto B

function quebra($coordenadas) {

    // Separa os valores
    $v = explode(",",$coordenadas);

    return $v;
}

// https://www.geodatasource.com/developers/php
function distance($lat1, $lon1, $lat2, $lon2, $unit) {

    $theta = $lon1 - $lon2;
    $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
    $dist = acos($dist);
    $dist = rad2deg($dist);
    $miles = $dist * 60 * 1.1515;
    $unit = strtoupper($unit);

    if ($unit == "K") {
        return ($miles * 1.609344);
    } else if ($unit == "N") {
        return ($miles * 0.8684);
    } else {
        return $miles;
    }
}

function calcula($c1Barco, $c2Barco, $cPontoA, $cPontoB) {

    // Quebra Latitude de Longitude
    $barcoC1 = quebra($c1Barco);
    $barcoC2 = quebra($c2Barco);
    $pontoA = quebra($cPontoA);
    $pontoB = quebra($cPontoB);

    // Calcula a distância entre os pontos
    $db1a = round(distance($barcoC1[0],$barcoC1[1],$pontoA[0],$pontoA[1],"K"),2);
    $db1b = round(distance($barcoC1[0],$barcoC1[1],$pontoB[0],$pontoB[1],"K"),2);
    $db2a = round(distance($barcoC2[0],$barcoC2[1],$pontoA[0],$pontoA[1],"K"),2);
    $db2b = round(distance($barcoC2[0],$barcoC2[1],$pontoB[0],$pontoB[1],"K"),2);

    // Tira a diferença da posição barco x pontos
    $dPA = $db1a - $db2a;
    $dPB = $db1b - $db2b;

    // Verifica a maior distância percorrida
    if ($dPA > $dPB)
        return "A";
    else
        return "B";
}

// Executa a função passando as variáveis
$resultado = calcula($c1_barco, $c2_barco, $c_pontoA, $c_pontoB);

if ($resultado == "A")
    echo "Destino: " . $n_pontoA;
else if ($resultado == "B")
    echo "Destino: " . $n_pontoB;
else
    echo "Erro!";

[Answer 1]

First you need to have well defined the distance formula, because they can contain positive and/or negative values, but you can solve it in a simple way.

If you are going to calculate "average speed", it is a little more complex than it seems, because as it will have 3 points, instead of 1, you would have to do the calculation separately.

Once this is done, you break the 2 values, do the calculations according to their definition, and then simply make the difference between them.

Role model:

// Coordenadas
$A = '-3.3105, -60.1694';
$B = '-3.3242, -60.6281';
$C = '-3.5970, -61.3930';

function distancia($ponto1, $ponto2) {

    // Separa os valores
    $p1 = explode(",",$ponto1);
    $p2 = explode(",",$ponto2);

    // Calcula a diferença
    $dLA = $p1[0] - $p2[0];
    $dLO = $p1[1] - $p2[1];

    echo '<br> LA: ' . $dLA . ' | LO: ' . $dLO;
}

distancia($A,$B);
distancia($B,$A);
distancia($A,$C);

Upshot:

LA: 0.0137 | LO: 0.4587
LA: -0.0137 | LO: -0.4587
LA: 0.2865 | LO: 1.2236

Function separating each coordinate to work with them outside a function:

function distancia2($ponto1, $ponto2) {

    // Separa os valores
    $p1 = explode(",",$ponto1);
    $p2 = explode(",",$ponto2);

    // Calcula a diferença
    $d["LA"] = $p1[0] - $p2[0];
    $d["LO"] = $p1[1] - $p2[1];

    return $d;
}

$dA_B = distancia2($A,$B);
$dB_C = distancia2($B,$C);
$dA_C = distancia2($A,$C);

echo '<br>' . $dA_B["LA"];
echo '<br>' . $dB_C["LA"];
echo '<br>' . $dA_C["LA"];
echo '<br>' . $dA_B["LO"];
echo '<br>' . $dB_C["LO"];
echo '<br>' . $dA_C["LO"];
  • But you take the coordinates and make the difference between them, you will have the position of the boat. With this 2nd function, just play in his formula.

  • Director take a look at this url http://grupoleopardo.com/app/kibarco/expresso-crystal-i/

  • @Grupoleopardo Very cool ! The "speed" part is already something more delicate. I will post on the answer

  • Here at the front I wanted to put in front to where you are going http://groupoilspar.com/app/kibarco

  • the speed part is already ok

  • has as agent talk through chat can be?

  • This application is already yours then ? I thought I was starting, and I wanted to play it. But then, you need to have your formula in order to apply in the script. Let’s assume that you today would calculate in hand, on paper and pen. How would you calculate this ? Having this in hand would already make what you need much easier. In view you will have 6 position variables (3 LA and 3 LO), and 6 distance (3 calculated distances, 3 previous distances).

  • Yes, I just don’t know how to create chat ! rs

  • I have little reputation still starting now more down here appeared to me more I can not start because my reputation is low yet I think rsrsss tries there

  • 1

    There’s a serious problem there: you’re calculating as if the planet is flat. I suggest you study geodesics

  • @Bacco I was going to talk about this, because once I was seeing about APP with GPS, and lucky that Android has class ready for this, already considering the curvature of the earth. That goes for the right speed !?

  • @Next Bacco: I have in the bank all the coordinates of the date and hour boat every 2 min that in case helps me to get where I want ?

  • https://chat.stackexchange.com/rooms/77036/roomw1d1d1wr4yw46u2

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.