Get Distance via php

tufanv

Expert
Licensed User
Longtime User
Hello,

I have a database on my mysql server that consists 2000 points with lat,lon values. What I want to do is , calculate point A 's distance to this 2000 points and put them in an array so i can get it via json. I searched the internet and found some calculation codes for php but they basicly calculates distance between two points. Is it possible to adapt those codes so that they can calculate all the distance for 2000 different points ? ,

An example code is here : https://www.geodatasource.com/developers/php

The sql query i will use in my php will start like this :
B4X:
$q = "SELECT latitude_deg,longitude_deg,id FROM places;
 
        $r = mysql_query($q);
        $all = array();
         while(($row = mysql_fetch_assoc($r))) {
                $all[] = $row;

                         }
                        print json_encode($all);

this code just gives mt he json of all places lat,lon values of course. What i want to do is run the function somewhere in this code and print all points lat lon values to a specific point via json. But i dont know how to modify my php code to run above function to print all the 2000 distances to my point as json.

Can you help me about it ?

TY
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
I did not understand the question correctly it seems. Can you describe it more clearly what exactly you have, what exactly you expect to get as result.
 

tufanv

Expert
Licensed User
Longtime User
I did not understand the question correctly it seems. Can you describe it more clearly what exactly you have, what exactly you expect to get as result.

Ok,
1)I have a Point A : Lets say latitude degree is : 41 and Longitude : 29
2) I have a database of 2000 different points with ids with different lat,lon values ( id:1,Lat:xx,lon:xx / id:2 Lat:xx , lon:xx ..... )

I want to calculate the distance of point A to all these 2000 points and send them as json array from php to my app. I have the above php code in my pho file now but it only sends the lat lon values of 2000 different points to my app. I dont need it , What i need is calculate those distances from point a and send them with their ids instead of lat,lon values.
 

tufanv

Expert
Licensed User
Longtime User
At last what i want to get is the id of the point and distance to my point A as json array
 

tufanv

Expert
Licensed User
Longtime User
Ok I managed to do it with the above funciton and below php code:

B4X:
    $q = "SELECT ident,latitude_deg,longitude_deg FROM points";
   
        $r = mysql_query($q);
        $all = array();
         while(($row = mysql_fetch_assoc($r))) {
              $lat2=$row['latitude_deg'];
                $lon2=$row['longitude_deg'];
                $ident=$row['ident'];

            //    $all[] = distance('29','41',$lat2,$lon2,'N');
                $arr[] = array('uzaklik' => distance('29','41',$lat2,$lon2,'N'), 'ident' => $ident);

                         }
                        print json_encode($arr);
 

tufanv

Expert
Licensed User
Longtime User
I have one little more question. I have the following code :
B4X:
     $new = array_filter($arr, function ($var) {
                   return ($var['uzaklik'] > '500' and $var['uzaklik'] < '1500');

this works fine.
if i put $value1 and $value2 instead of '500' and '1500' i am getting null. ($value1 is 500 and $value2 is 1500 ) . It must not give null but i don't understand any ideas ?
 

ilan

Expert
Licensed User
Longtime User
i think int's should not be in '...'
just remove '

B4X:
$value1 = 500
$value2 = 1500

$new = array_filter($arr, function ($var) {
                   return ($var['uzaklik'] > $value1 and $var['uzaklik'] < $value2);
 

tufanv

Expert
Licensed User
Longtime User
i think int's should not be in '...'
just remove '

B4X:
$value1 = 500
$value2 = 1500

$new = array_filter($arr, function ($var) {
                   return ($var['uzaklik'] > $value1 and $var['uzaklik'] < $value2);
it is giving null when i use like that.
It only works when they are in single quote and a value like i post :
return ($var['uzaklik'] > '500' and $var['uzaklik'] < '1500');

I changed '500' and '1500' to $value1 and $value2 as stated.. no luck.
 

tufanv

Expert
Licensed User
Longtime User
ok this the whole code :

B4X:
    Case "24": //yakınklarıal
    $usn = clean($_GET["girdi1"]);
    $psw = md5(clean($_GET["girdi2"]));
    $base = clean($_GET["girdi3"]);
    $p1 = clean($_GET["girdi4"]);
    $p2 = clean($_GET["girdi5"]);
    $value1 = 500;
    $value2 = 1500;








        $q = "SELECT ident,latitude_deg,longitude_deg,iso_country,name FROM airports3";
    //    $q = "SELECT * FROM useraircrafts1 where username='".$usn."' order by date DESC";
        $r = mysql_query($q);
        $all = array();
         while(($row = mysql_fetch_assoc($r))) {
              $lat2=$row['latitude_deg'];
                $lon2=$row['longitude_deg'];
                $ident=$row['ident'];
                $ulke=$row['iso_country'];
                $name=$row['name'];

            //    $all[] = distance('29','41',$lat2,$lon2,'N');
                $arr[] = array('uzaklik' => distance('41','26',$lat2,$lon2,'N'), 'ident' => $ident, 'ulke' => $ulke, 'name' => $name);

                         }
                         $uzaklik = array();
             foreach ($arr as $key => $row2)
               {
                 $uzaklik[$key] = $row2['uzaklik'];
                     }
                   array_multisort($uzaklik, SORT_ASC, $arr);

                                     $new = array_filter($arr, function ($var) {
                   return ($var['uzaklik'] > '500' and $var['uzaklik'] < '1500');
                    });

                        print json_encode(array_slice($new, 0, 10000));

This code gives the distances which is between 500 and 1500
B4X:
  return ($var['uzaklik'] > '500' and $var['uzaklik'] < '1500');
without any problem. What i want to do normally is to get the to value from my app which is :
B4X:
  $p1 = clean($_GET["girdi4"]);
    $p2 = clean($_GET["girdi5"]);

but it always gives null because both are them are taken as 0 altough they are not . I send them as 500 and 1500 from the app. So ı said ok lets not take the values from the app maybe there is a problem and declare them in the php as you suggested and used:
B4X:
    $value1 = 500;
    $value2 = 1500;
and changed to :

B4X:
return ($var['uzaklik'] > $value1 and $var['uzaklik'] < $value2);

and this gives null again.
 

ilan

Expert
Licensed User
Longtime User
You are checking for STRING with 500 or 1500 in content.

it is giving null when i use like that.
It only works when they are in single quote and a value like i post :

could it be that $var['uzaklik'] is not always a valid int?

if you do it like this:

B4X:
$value1 = '500';
    $value2 = '1500';

B4X:
return ($var['uzaklik'] > '$value1' and $var['uzaklik'] < '$value2');

does it works??

if yes then $var['uzaklik'] is sometime (or always i don't know) a string and thats why you get a valid boolean returned because (a > b and a < c) = false true

try to get a list of all your $var['uzaklik'] for every raw and see if all are valid ints
 
Last edited:

tufanv

Expert
Licensed User
Longtime User
could it be that $var['uzaklik'] is not always a valid int?

if you do it like this:

B4X:
$value1 = '500';
    $value2 = '1500';

B4X:
return ($var['uzaklik'] > '$value1' and $var['uzaklik'] < '$value2');

does it works??

if yes then $var['uzaklik'] is sometime (or always i don't know) a string and thats why you get a valid boolean returned because (a > b and a < c) = false true

try to get a list of all your $var['uzaklik'] for every raw and see if all are valid ints


New
It is not working. I also tried to remove single qotes while trying because with single qutoes it is not working. Both way not working. I will try to check if int. But it is very strange i cant get it work :)
return ($var['uzaklik'] > '$value1' and $var['uzaklik'] < '$value2');
 

tufanv

Expert
Licensed User
Longtime User
Dump the array to see the real types & values in the array:

B4X:
var_dump($uzaklik)
$uzaklik is just to filter the $arr to $new . Should i still dump it ? if yes how can i do it and how can i see it ? I just can see the json inside my app ?
 

KMatle

Expert
Licensed User
Longtime User
So just dump the arrays with the data (it's a simple way to check the values&types):

B4X:
$a = array(1, 2, array("a", "b", "c"));
var_dump($a);

just modify the code and call the script via browser (or see the job's response).

The output is:

B4X:
array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  array(3) {
    [0]=>
    string(1) "a"
    [1]=>
    string(1) "b"
    [2]=>
    string(1) "c"
  }
}

It just shows what type the value is. So if you compare a string (even if it looks like an integer) with a real integer may cause your problem. Take a look at the database, too. Are the culumns integers? Sure?
 
Top