[Go] PHP calculates the distance between two coordinates, Calculate the Distance between two Points in PHP

Source: Internet
Author: User
Tags cos

Calculate the Distance between the Points in PHP

There is a lot of applications where it's useful to know the distance between the coordinates. Here, you'll find a PHP function that takes the latitude and longitude of the points and returns the distance between them In both miles and metric units.

You can also use this to find the distance between and addresses by taking advantage of the Google geotargetting API.

Here ' s the function:

functionGet_distance_between_points ($latitude 1,$longitude 1,$latitude 2,$longitude 2) {$theta=$longitude 1-$longitude 2;$miles= (Sin(Deg2rad($latitude 1)) *Sin(Deg2rad($latitude 2))) + (Cos(Deg2rad($latitude 1)) *Cos(Deg2rad($latitude 2)) *Cos(Deg2rad($theta)));$miles=ACOs($miles);$miles=rad2deg($miles);$miles=$miles* 60 * 1.1515;$feet=$miles* 5280;$yards=$feet/3;$kilometers=$miles* 1.609344;$meters=$kilometers* 1000;return Compact(' Miles ', ' feet ', ' yards ', ' kilometers ', ' meters '); }

Call
And here's an example of the function in action, using the coordinates in New York city:

$point 1=Array(' lat ' = 40.770623, ' long ' = 73.964367);$point 2=Array(' lat ' = 40.758224, ' long ' = 73.917404);$distance= Get_distance_between_points ($point 1[' Lat '],$point 1[' Long '],$point 2[' Lat '],$point 2[' Long ']);
foreach($distance as $unit=$value) { Echo $unit.‘: ‘.Number_format($value, 4). ' <br/> '; }

The example returns the following:

miles:2.6025//Miles
feet:13,741.4350//FT
yards:4,580.4783//Yards
kilometers:4.1884//km (miles)
meters:4,188.3894//M (m)

from:https://inkplant.com/code/calculate-the-distance-between-two-points.php

[Go] PHP calculates the distance between two coordinates, Calculate the Distance between two Points in PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.