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 8079803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:13:33+00:00 2026-06-05T16:13:33+00:00

I have a function in PHP which calculates the distance between two places. Here

  • 0

I have a function in PHP which calculates the distance between two places.

Here is the php code :

function distance($lat1, $lon1, $lat2, $lon2) {
   $earth_radius = 6371; 
   $delta_lat = $lat2 - $lat1 ;
   $delta_lon = $lon2 - $lon1 ;

  $distance  = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($delta_lon)) ;
  $distance  = acos($distance);
  $distance  = rad2deg($distance);
  $distance  = $distance * 60 * 1.1515;
  $distance  = round($distance, 4);
  return $distance = $distance * 1.609344;
 }

It gives good calculations around 25 km, but around 500 km the calculations are wrong.
My other question is it really giving miles or kilometers?

For example this map gives a distance of 443 km from Morbi to Surat, but the function gives me a result of 274 km

  • 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-06-05T16:13:35+00:00Added an answer on June 5, 2026 at 4:13 pm

    Ok, now that’s clear that you want the road distance, we can give you a proper answer, to get the result, you can use the Google API Distance Matrix, you have different options and parameters, you have to figure out which suits you the most, be aware that there’s some limitation (for the free edition):

    <<
    The Distance Matrix API has the following limits in place:
    100 elements per query.
    100 elements per 10 seconds.
    2 500 elements per 24 hour period.
    >>
    

    However for the purpose of answering your question i wrote a simple PHP script that gets a XML file and parse the distance/duration with SimpleXMLElement …

    <?php
    $start = "morbi";   // You can either set lon and lat separated with a comma ex: 22.814162,70.834351
    $destination = "surat";
    $mode = "driving";  // Different modes: driving,walking,bicycling (be aware that some of the modes aren't available for some poi's ...)
    
    $xml = file_get_contents("http://maps.googleapis.com/maps/api/distancematrix/xml?origins=$start&destinations=$destination&mode=$mode&language=en-EN&sensor=false");
    $data = new SimpleXMLElement($xml);
    $distance = $data->row->element->distance->text;
    $time = $data->row->element->duration->text;
    if(empty($distance) OR empty($time)){
        echo "Oops, this mode ($mode) isn't available ...";
    }else{
        echo "The distance between $start and $destination is $distance and the travel duration while $mode is $time .";
    }
    ?>
    

    Hope this was helpful 🙂

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

Sidebar

Related Questions

I have a PHP function which requires 2 arguments . The function calculates the
in php i wrote my own debug function which have two arguments: text and
i have a php function which returns a random json encoded color <?php function
I have a PHP function which inserts multiple records into MySQL: function commit_purchase($asset_type_ID, $org_ID,
I have a load of php templates which uses a custom translate function __,
I have a ajax function in index.php which calls on the page thread.php which
I want to have a function written in PHP which can create anonymous functions
I have a PHP function which returns an array : function lire( $id) {
I have this HTTP Post function in php which sends a form to a
I have a php function which displays a rating bar with the arguments. I

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.