{"id":136,"date":"2014-11-29T13:02:29","date_gmt":"2014-11-29T13:02:29","guid":{"rendered":"https:\/\/www.kolkataonweb.com\/code-bank\/?p=136"},"modified":"2014-11-25T17:10:24","modified_gmt":"2014-11-25T17:10:24","slug":"distance-between-two-latitudes-and-longitudes-part-2","status":"publish","type":"post","link":"https:\/\/www.kolkataonweb.com\/code-bank\/miscellaneous\/distance-between-two-latitudes-and-longitudes-part-2\/","title":{"rendered":"Distance between two Latitudes and Longitudes \u2013 Part 2"},"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>Javascript 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\tvar radlat1 = Math.PI * lat1\/180;\r\n\tvar radlat2 = Math.PI * lat2\/180;\r\n\tvar radlon1 = Math.PI * lon1\/180;\r\n\tvar radlon2 = Math.PI * lon2\/180;\r\n\tvar theta = lon1-lon2;\r\n\tvar radtheta = Math.PI * theta\/180;\r\n\r\n\tvar dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);\r\n\r\n\tdist = Math.acos(dist);\r\n\tdist = dist * 180\/Math.PI;\r\n\tdist = dist * 60 * 1.1515;\r\n\tif (unit==\"K\") { dist = dist * 1.609344 }\r\n\tif (unit==\"N\") { dist = dist * 0.8684 }\r\n\treturn dist;\r\n}        \r\n\r\ndocument.write(distance(32.9697, -96.80322, 29.46786, -98.53506, \"K\") + \" Kilometers\");\r\n          \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 Javascript 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-2\/\">Continue reading <span class=\"screen-reader-text\">Distance between two Latitudes and Longitudes \u2013 Part 2<\/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-136","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\/136","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=136"}],"version-history":[{"count":3,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts\/136\/revisions"}],"predecessor-version":[{"id":140,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts\/136\/revisions\/140"}],"wp:attachment":[{"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/media?parent=136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/categories?post=136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/tags?post=136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}