{"id":144,"date":"2014-12-01T17:14:18","date_gmt":"2014-12-01T17:14:18","guid":{"rendered":"https:\/\/www.kolkataonweb.com\/code-bank\/?p=144"},"modified":"2014-11-25T17:19:33","modified_gmt":"2014-11-25T17:19:33","slug":"distance-between-two-latitudes-and-longitudes-part-3","status":"publish","type":"post","link":"https:\/\/www.kolkataonweb.com\/code-bank\/miscellaneous\/distance-between-two-latitudes-and-longitudes-part-3\/","title":{"rendered":"Distance between two Latitudes and Longitudes &#8211; Part 3"},"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>C 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\n#include <math.h>\r\n\r\n#define pi 3.14159265358979323846\r\n\r\ndouble distance(double lat1, double lon1, double lat2, double lon2, char unit) \r\n{\r\n  double theta, dist;\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  dist = dist * 60 * 1.1515;\r\n  switch(unit) \r\n {\r\n    case 'M':\r\n      break;\r\n    case 'K':\r\n      dist = dist * 1.609344;\r\n      break;\r\n    case 'N':\r\n      dist = dist * 0.8684;\r\n      break;\r\n  }\r\n  return (dist);\r\n}\r\n\r\n\/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*\/\r\n\/*::  This function converts decimal degrees to radians             :*\/\r\n\/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*\/\r\ndouble deg2rad(double deg) {\r\n  return (deg * pi \/ 180);\r\n}\r\n\r\n\/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*\/\r\n\/*::  This function converts radians to decimal degrees             :*\/\r\n\/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*\/\r\ndouble rad2deg(double rad) {\r\n  return (rad * 180 \/ pi);\r\n}\r\n\r\nprintf(\"%f\",distance(32.9697, -96.80322, 29.46786, -98.53506, \"K\"));\r\nputs(\" Kilometers\");\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 C 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\/miscellaneous\/distance-between-two-latitudes-and-longitudes-part-3\/\">Continue reading <span class=\"screen-reader-text\">Distance between two Latitudes and Longitudes &#8211; Part 3<\/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":[1],"tags":[],"class_list":["post-144","post","type-post","status-publish","format-standard","hentry","category-miscellaneous","entry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts\/144","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=144"}],"version-history":[{"count":1,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts\/144\/revisions"}],"predecessor-version":[{"id":145,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts\/144\/revisions\/145"}],"wp:attachment":[{"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/media?parent=144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/categories?post=144"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/tags?post=144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}