CodeIgniter Forums
Get distance of locations - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Get distance of locations (/showthread.php?tid=72050)



Get distance of locations - omid_student - 10-30-2018

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?


RE: Get distance of locations - Pertti - 10-30-2018

Try

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

Or is the maths part wrong?


RE: Get distance of locations - omid_student - 10-30-2018

(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?


RE: Get distance of locations - omid_student - 10-30-2018

(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


RE: Get distance of locations - Pertti - 10-30-2018

(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


RE: Get distance of locations - omid_student - 10-30-2018

(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


RE: Get distance of locations - Pertti - 10-30-2018

(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