• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Get distance of locations omid_student Omid Aghakhani Posts: 344 Threads: 103 Joined: May 2015 Reputation: 0 10-30-2018, 01:18 AM 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? Pertti Senior Member Posts: 365 Threads: 5 Joined: May 2015 Reputation: 32 10-30-2018, 01:54 AM Try PHP Code:`if (!is_null(\$current)) ` Or is the maths part wrong? omid_student Omid Aghakhani Posts: 344 Threads: 103 Joined: May 2015 Reputation: 0 10-30-2018, 01:59 AM (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? omid_student Omid Aghakhani Posts: 344 Threads: 103 Joined: May 2015 Reputation: 0 10-30-2018, 02:06 AM (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 Pertti Senior Member Posts: 365 Threads: 5 Joined: May 2015 Reputation: 32 10-30-2018, 03:16 AM (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 omid_student Omid Aghakhani Posts: 344 Threads: 103 Joined: May 2015 Reputation: 0 10-30-2018, 05:14 AM (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 Sorry my problem was solved My locations not arranged by asc or desc Thank you Pertti Senior Member Posts: 365 Threads: 5 Joined: May 2015 Reputation: 32 10-30-2018, 05:44 AM (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