• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get distance of locations

#1
Hi
I have many locations(about 1000 location)
I need calculate distance between this location
My code is :
PHP Code:
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 (
strtolower($unit) == "k") {
            return (
$miles 1.609344);
        } else if (
$unit == "N") {
            return (
$miles 0.8684);
        } else {
            return 
$miles;
        }
    } 

I calculate distance between two location with foreach
PHP Code:
foreach($result as $row) {

            if (
$current != NULL) {
                
$distance += distance($current['latitude'],$current['longitude'],$row['latitude'],$row['longitude'],'k') ;
                
$current    =   $row;
            } else {
                
$current    =   $row;
            }

        } 

In each loop,i save previous location and get distance from their
But it is wrong
Does anybody can help me?
Reply

#2
Try

PHP Code:
if (!is_null($current)) 

Or is the maths part wrong?
Reply

#3
(10-30-2018, 01:54 AM)Pertti Wrote: Try

PHP Code:
if (!is_null($current)) 

Or is the maths part wrong?
"is the maths part wrong?" what do you mean?
Reply

#4
(10-30-2018, 01:54 AM)Pertti Wrote: Try

PHP Code:
if (!is_null($current)) 

Or is the maths part wrong?

Your code did not work
Reply

#5
(10-30-2018, 01:59 AM)omid_student Wrote: "is the maths part wrong?" what do you mean?

I'm just trying to understand what part you have problem with from your original post.

As far as I can see, from the source provided, there could be only two issues - either the calculation between two points is incorrect, or you have issues with feeding in correct points.

You should try to specify what and why you think it's not working, and it'll be easier for us to help you, that's all Smile
Reply

#6
(10-30-2018, 03:16 AM)Pertti Wrote:
(10-30-2018, 01:59 AM)omid_student Wrote: "is the maths part wrong?" what do you mean?

I'm just trying to understand what part you have problem with from your original post.

As far as I can see, from the source provided, there could be only two issues - either the calculation between two points is incorrect, or you have issues with feeding in correct points.

You should try to specify what and why you think it's not working, and it'll be easier for us to help you, that's all Smile

Sorry my problem was solved
My locations not arranged by asc or desc
Thank you
Reply

#7
(10-30-2018, 05:14 AM)omid_student Wrote: Sorry my problem was solved
My locations not arranged by asc or desc
Thank you

Excellent Smile
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.