Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7068839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:22:22+00:00 2026-05-28T05:22:22+00:00

I just can’t seem to figure out how to make efficient and clean looking

  • 0

I just can’t seem to figure out how to make efficient and clean looking code for figuring this out.

I have a string of coordinates, each point is made up of Longitude, Latitude and Altitude naturally (I am not worried about altitude at all right now and I know the function I have for figuring out the footage does not support altitude):

$coordinates = "-82.36554110283872,26.15200821551467,0 -82.420692,26.097404,0 -82.52855700000001,26.186567,0 -82.41250599999999,25.996422,0 -82.50644510379755,26.05431354409091,0" 

I need to find out the distance in feet between each coordinate and add up the total.
I have the following function(which works beautifully) to figure out the distance between two points:

function coordDistance($lat1, $lon1, $lat2, $lon2) {
$delta_lat = $lat2 - $lat1;
$delta_lon = $lon2 - $lon1;

$earth_radius = 20908800.00; //Distance around the earth in feet

$alpha  = $delta_lat/2;
$beta   = $delta_lon/2;
$a          = sin(deg2rad($alpha)) * sin(deg2rad($alpha)) + cos(deg2rad($lat1)) *    cos(deg2rad($lat2)) * sin(deg2rad($beta)) * sin(deg2rad($beta)) ;
$c      = asin(min(1, sqrt($a)));
$distance = 2*$earth_radius * $c;
$distance = round($distance, 4);

return $distance;
}

The way I am currently breaking down my coordinates string and attempting to pass it to the function is completely ludicrous. Any suggestions on how to split the coordinates and break them down to get the total footage for the path in a nice way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:22:22+00:00Added an answer on May 28, 2026 at 5:22 am

    Essentially a variable to accumulate into. Splitting the string on space to get individual point. Looping for n-1. Calculating the distance between i and i+1.

    $total = 0;
    
    $points = explode(' ', $coordinates);
    $count = count($points);
    
    for ($i = 0; $count - 1 > $i; ++$i) {
        list($lon1, $lat1, $alt1) = explode(',', $points[$i]);
        list($lon2, $lat2, $alt2) = explode(',', $points[$i + 1]);
    
        $total += coordDistance($lat1, $lon1, $lat2, $lon2);
    }
    

    I didn’t quite test the code. Seems to me like your coordinate string has an extra space in it after a minus sign.

    Make sure lon and lat are read in the right order too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to regular expression and just can't seem to figure this out: '/^[A-Za-z0-9](?:.[A-Za-z0-9]+)$/'
I just can't figure this out, for some reason this: $string = #mainparent {
I just can't seem to figure this one out... As far as I know,
I just can not figure this out, or find any docs. I have a
I just can't figure this out. I have a script that gets data from
I just can't seem to figure this out - I'm trying to get the
I just can't figure it out. I have this shape in res/drawable/start_light.xml <?xml version=1.0
I just can't figure out how do I make sure an argument passed to
I'm new to prolog and I just can't figure this out. I'm trying to
I just can't work this one out. I have a table with a column

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.