{"id":132,"date":"2014-11-28T05:36:52","date_gmt":"2014-11-28T05:36:52","guid":{"rendered":"https:\/\/www.kolkataonweb.com\/code-bank\/?p=132"},"modified":"2014-11-25T17:11:03","modified_gmt":"2014-11-25T17:11:03","slug":"distance-between-two-latitudes-and-longitudes-part-1","status":"publish","type":"post","link":"https:\/\/www.kolkataonweb.com\/code-bank\/php\/distance-between-two-latitudes-and-longitudes-part-1\/","title":{"rendered":"Distance between two Latitudes and Longitudes &#8211; Part 1"},"content":{"rendered":"<p>This series is about code in various languages that will calculate the distance between two latitudes and longitudes. This article is for the <strong>PHP code<\/strong>.<\/p>\n<p><strong>Code courtsey<\/strong> : <a href=\"http:\/\/www.geodatasource.com\" title=\"GeoDataSource\" target=\"_blank\">GeoDataSource<\/a><\/p>\n<pre class='wp-code-highlight prettyprint'>\r\n\/*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*\/\r\n\/*::                                                             :*\/\r\n\/*::  This routine calculates the distance between two           :*\/\r\n\/*::  points (given the latitude\/longitude of those points).     :*\/\r\n\/*::  It is being used to calculate the distance between two     :*\/\r\n\/*::  locations using GeoDataSource(TM) Products                 :*\/\r\n\/*::                                                             :*\/\r\n\/*::  Definitions:                                               :*\/\r\n\/*::    South latitudes are negative,                            :*\/\r\n\/*::    east longitudes are positive                             :*\/\r\n\/*::                                                             :*\/\r\n\/*::  Passed to function:                                        :*\/\r\n\/*::    lat1, lon1 = Latitude and Longitude of point 1           :*\/\r\n\/*::                 (in decimal degrees)                        :*\/\r\n\/*::    lat2, lon2 = Latitude and Longitude of point 2           :*\/\r\n\/*::                 (in decimal degrees)                        :*\/\r\n\/*::    unit = the unit you desire for results                   :*\/\r\n\/*::           where: 'M' is statute miles                       :*\/\r\n\/*::                  'K' is kilometers (default)                :*\/\r\n\/*::                  'N' is nautical miles                      :*\/\r\n\/*::  Worldwide cities and other features databases with         :*\/\r\n\/*::  latitude longitude are available at                        :*\/\r\n\/*::  http:\/\/www.geodatasource.com                               :*\/ \r\n\/*::                                                             :*\/\r\n\/*::  For enquiries, please contact sales@geodatasource.com      :*\/\r\n\/*::                                                             :*\/\r\n\/*::  Official Web site: http:\/\/www.geodatasource.com            :*\/\r\n\/*::                                                             :*\/\r\n\/*::         GeoDataSource.com (C) All Rights Reserved 2014      :*\/\r\n\/*::                                                             :*\/\r\n\/*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*\/\r\n\r\nfunction distance($lat1, $lon1, $lat2, $lon2, $unit) {\r\n\r\n  $theta = $lon1 - $lon2;\r\n  \r\n  $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));\r\n  \r\n  $dist = acos($dist);\r\n  $dist = rad2deg($dist);\r\n  $miles = $dist * 60 * 1.1515;\r\n  $unit = strtoupper($unit);\r\n\r\n  if ($unit == \"K\") \r\n  {\r\n    return ($miles * 1.609344);\r\n  } \r\n  else \r\n  \tif ($unit == \"N\") \r\n  \t{\r\n     \t  return ($miles * 0.8684);\r\n   \t} \r\n    else \r\n    {\r\n        return $miles;\r\n    }\r\n}\r\n\r\necho distance(32.9697, -96.80322, 29.46786, -98.53506, \"M\") . \" Miles<br>\";\r\necho distance(32.9697, -96.80322, 29.46786, -98.53506, \"K\") . \" Kilometers<br>\";\r\necho distance(32.9697, -96.80322, 29.46786, -98.53506, \"N\") . \" Nautical Miles<br>\";\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This series is about code in various languages that will calculate the distance between two latitudes and longitudes. This article is for the PHP code. Code courtsey : GeoDataSource \/*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*\/ \/*:: :*\/ \/*:: This routine calculates the distance between two :*\/ \/*:: points (given the latitude\/longitude of those points). :*\/ \/*:: It is being used&hellip; <a class=\"more-link\" href=\"https:\/\/www.kolkataonweb.com\/code-bank\/php\/distance-between-two-latitudes-and-longitudes-part-1\/\">Continue reading <span class=\"screen-reader-text\">Distance between two Latitudes and Longitudes &#8211; Part 1<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-132","post","type-post","status-publish","format-standard","hentry","category-php","entry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts\/132","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/comments?post=132"}],"version-history":[{"count":3,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts\/132\/revisions"}],"predecessor-version":[{"id":141,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts\/132\/revisions\/141"}],"wp:attachment":[{"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/media?parent=132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/categories?post=132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/tags?post=132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}