Code Snippets

  

PHP Source Code


Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 99,781 PHP Programmers for FREE! Ask your question and get quick answers from experts. There are 1,534 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!




Calculate distance between 2 points

This is a snippet for calculating the distance between 2 points. Each "point" is represented by the longitude & latitude for that location

Submitted By: PsychoCoder
Actions:
Rating:
Views: 211

Language: PHP

Last Modified: May 14, 2008
Instructions: Pass the function the longitude/latitude for each location and a type (M for miles, K for kilometers)

Snippet


  1. function CalculateDistance($lat1, $lon1, $lat2, $lon2, $distanceType)
  2. {
  3.  
  4.   $theta = $lon1 - $lon2;
  5.   $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2))cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
  6.   $dist = acos($dist);
  7.   $dist = rad2deg($dist);
  8.   $miles = $dist * 60 * 1.1515;
  9.   $distanceType = strtoupper($distanceType);
  10.  
  11.   if ($distanceType == "K")
  12.   {
  13.     //return distance in kilometers
  14.     return ($miles * 1.609344);
  15.   }
  16.      else
  17.      {
  18.           //return distance in miles
  19.           return $miles;
  20.      }
  21. }

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month
-->