{"id":39,"date":"2008-02-17T16:46:35","date_gmt":"2008-02-17T21:46:35","guid":{"rendered":"http:\/\/jaimerios.com\/?p=39"},"modified":"2025-12-02T23:51:08","modified_gmt":"2025-12-02T23:51:08","slug":"calculating-distance-between-2-points-on-earth-using-the-haversine-formula","status":"publish","type":"post","link":"https:\/\/jaimerios.com\/?p=39","title":{"rendered":"Calculating distance between 2 points on Earth using the Haversine formula"},"content":{"rendered":"\n<p><strong>2016 Update<\/strong><br>The code and the Xcode project for this article is now also available on <a href=\"https:\/\/github.com\/AhiyaHiya\/haversine\" target=\"_blank\" rel=\"noopener\">GitHub<\/a>.<\/p>\n\n\n\n<p>A while ago, I worked on a project where I had to implement code in C\/C++ that could calculate the distance between 2 locations within the United States. It would be nice if the Earth was flat, since this would make my job a little easier, but the Earth is round and I needed to find a way that I could calculate distance. From the research that I have done, I found that plenty of people have worked on this problem before and one popular article is sited over and over again is this: R. W. Sinnott, &#8220;Virtues of the Haversine&#8221;, Sky and Telescope 68 (2), 159 (1984).<\/p>\n\n\n\n<p>The Math used in all of the articles I read refer to the Haversine formula. Every example I found of this formula were either written in plain Math or shown in JavaScript. Although it was nice finding examples of this in JavaScript, there were a couple of problems with this:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>C datatypes aren\u2019t the same as JavaScript datatypes and<\/li>\n\n\n\n<li>All examples used Latitude and Longitude, but I had to use Zip codes<\/li>\n<\/ol>\n\n\n\n<p>So I had some more hurdles to overcome. They didn\u2019t seem like big ones, but ones that I had to overcome nonetheless.<\/p>\n\n\n\n<p>Now the funny thing about this project was that this was one of those endeavors where a spec wasn\u2019t available before or during development. As a consequence, I would have designed the code differently than what I had have delivered. Not that there is anything wrong with what I ended up with, it\u2019s just that I like neat code and what I wrote for this article is what I would have done if I had known all of the variables.<\/p>\n\n\n\n<p><strong>Example Code<\/strong><\/p>\n\n\n\n<p>What I am going to show you is how to calculate distance between two points within the United States, using the Haversine formula, implemented in C. The code will show you how to do that using Latitude and Longitude coordinates. The example application that is posted will show you how to use Zip codes in place of Latitude and Longitude.<\/p>\n\n\n\n<p>The example code is demonstrated in an application written for Mac OS X. Win32 developers shouldn\u2019t have to worry, since the Math code is done in a separate C file. Objective-C is used only for the UI file so you won\u2019t have to read my Objective-C code to understand how to implement this formula in your own application. For the zip code implementation, I use the standard template library to contain the information I need in map so that I can extract the Latitude and Longitude for a particular postal code.<\/p>\n\n\n\n<p>Creating the example code required these steps:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Creating a Cocoa Application<\/li>\n\n\n\n<li>Creating the Haversine C code and<\/li>\n\n\n\n<li>Creating code that can handle Zip codes and finally<\/li>\n\n\n\n<li>Creating a routine to check the validity of the output<\/li>\n<\/ol>\n\n\n\n<p><strong>Creating a Cocoa Application<\/strong><\/p>\n\n\n\n<p>This is pretty simple step that most Apple developers are familiar with. I\u2019m using Xcode 3.0, however, I set the project settings to be compatible with Xcode 2.4. Once the Cocoa application was created, I needed to create an interface that would take my Longitude and Latitude parameters and display the resulting output.<\/p>\n\n\n\n<p>I also created a section that would take in Zip codes and get the result that way too! The logic is pretty simple and contained within the Objective-C code.<\/p>\n\n\n\n<p><strong>Creating the Haversine C code<\/strong><\/p>\n\n\n\n<p>This part was a little tough to work with. Every example I saw on the web used JavaScript. Here is the Math and JavaScript code I found at <a title=\"http:\/\/www.movable-type.co.uk\/scripts\/latlong.html\" href=\"http:\/\/www.movable-type.co.uk\/scripts\/latlong.html\" target=\"_blank\" rel=\"noopener\">http:\/\/www.movable-type.co.uk\/scripts\/latlong.html<\/a> :<\/p>\n\n\n\n<p><strong>Haversine Formula:<\/strong><\/p>\n\n\n\n<div>\n<div>\u03c6 is latitude<\/div>\n<div>\u03bb is longitude<\/div>\n<div>R is earth\u2019s radius (mean radius = 6,371km)<\/div>\n<div>a = sin\u00b2(\u0394\u03c6\/2) + cos \u03c61 \u22c5 cos \u03c62 \u22c5 sin\u00b2(\u0394\u03bb\/2)<\/div>\n<div>c = 2 \u22c5 atan2( \u221aa, \u221a(1\u2212a) )<\/div>\n<div>d = R \u22c5 c<\/div>\n<\/div>\n\n\n\n<p>(Note that angles need to be in radians to pass to trig functions).<\/p>\n\n\n\n<p><strong>JavaScript Implementation:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 0 16px;font-size:0.8em;width:100%;text-align:left;background-color:#1E1E1E;font-style:italic;color:#D4D4D4\"><span style=\"border-bottom:1px solid rgba(234, 191, 191, 0.2)\">JavaScript<\/span><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>var R = 6371; \/\/ km\nvar dLat = (lat2-lat1).toRad();\nvar dLon = (lon2-lon1).toRad();\nvar a = Math.sin(dLat\/2) * Math.sin(dLat\/2) +\n\nMath.cos(lat1.toRad()) * Math.cos(lat2.toRad()) * Math.sin(dLon\/2) * Math.sin(dLon\/2);\n\nvar c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));\nvar d = R * c;<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">var<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4FC1FF\">R<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #B5CEA8\">6371<\/span><span style=\"color: #D4D4D4\">; <\/span><span style=\"color: #6A9955\">\/\/ km<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">var<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">dLat<\/span><span style=\"color: #D4D4D4\"> = (<\/span><span style=\"color: #9CDCFE\">lat2<\/span><span style=\"color: #D4D4D4\">-<\/span><span style=\"color: #9CDCFE\">lat1<\/span><span style=\"color: #D4D4D4\">).<\/span><span style=\"color: #DCDCAA\">toRad<\/span><span style=\"color: #D4D4D4\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">var<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">dLon<\/span><span style=\"color: #D4D4D4\"> = (<\/span><span style=\"color: #9CDCFE\">lon2<\/span><span style=\"color: #D4D4D4\">-<\/span><span style=\"color: #9CDCFE\">lon1<\/span><span style=\"color: #D4D4D4\">).<\/span><span style=\"color: #DCDCAA\">toRad<\/span><span style=\"color: #D4D4D4\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">var<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">a<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #9CDCFE\">Math<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">sin<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">dLat<\/span><span style=\"color: #D4D4D4\">\/<\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">) * <\/span><span style=\"color: #9CDCFE\">Math<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">sin<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">dLat<\/span><span style=\"color: #D4D4D4\">\/<\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">) +<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #9CDCFE\">Math<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">cos<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">lat1<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">toRad<\/span><span style=\"color: #D4D4D4\">()) * <\/span><span style=\"color: #9CDCFE\">Math<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">cos<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">lat2<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">toRad<\/span><span style=\"color: #D4D4D4\">()) * <\/span><span style=\"color: #9CDCFE\">Math<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">sin<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">dLon<\/span><span style=\"color: #D4D4D4\">\/<\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">) * <\/span><span style=\"color: #9CDCFE\">Math<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">sin<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">dLon<\/span><span style=\"color: #D4D4D4\">\/<\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">var<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">c<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\"> * <\/span><span style=\"color: #9CDCFE\">Math<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">atan2<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">Math<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">sqrt<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">a<\/span><span style=\"color: #D4D4D4\">), <\/span><span style=\"color: #9CDCFE\">Math<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">sqrt<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">-<\/span><span style=\"color: #9CDCFE\">a<\/span><span style=\"color: #D4D4D4\">));<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">var<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">d<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #4FC1FF\">R<\/span><span style=\"color: #D4D4D4\"> * <\/span><span style=\"color: #9CDCFE\">c<\/span><span style=\"color: #D4D4D4\">;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p><strong>Now for the C code!<\/strong><\/p>\n\n\n\n<p>To convert this code, there were three things I had to do to implement this as C code: A. Include the &lt;math.h&gt; file into my C file so that I get pi. B. I had to convert our Latitude and Longitude differences into radians, by multiplying the result by ( pi \/ 180 ) and C. I used the pow function to get the squared value instead of doing multiplying my value twice. Here is what I got:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 0 16px;font-size:0.8em;width:100%;text-align:left;background-color:#1E1E1E;font-style:italic;color:#D4D4D4\"><span style=\"border-bottom:1px solid rgba(234, 191, 191, 0.2)\">C<\/span><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>double CalculateDistance( double nLat1, double nLon1, double nLat2, double nLon2 )\n{\n    int nRadius = 6371; \/\/ Earth's radius in Kilometers\n    \/\/ Get the difference between our two points\n    \/\/ then convert the difference into radians\n    double nDLat = (nLat2 - nLat1) * (M_PI\/180);\n    double nDLon = (nLon2 - nLon1) * (M_PI\/180);\n    double nA = pow ( sin(nDLat\/2), 2 ) + cos(nLat1) * cos(nLat2) * pow ( sin(nDLon\/2), 2 );\n\n    double nC = 2 * atan2( sqrt(nA), sqrt( 1 - nA ));\n    double nD = nRadius * nC;\n\n    return nD; \/\/ Return our calculated distance\n}<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">double<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">CalculateDistance<\/span><span style=\"color: #D4D4D4\">( <\/span><span style=\"color: #569CD6\">double<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">nLat1<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #569CD6\">double<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">nLon1<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #569CD6\">double<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">nLat2<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #569CD6\">double<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">nLon2<\/span><span style=\"color: #D4D4D4\"> )<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">int<\/span><span style=\"color: #D4D4D4\"> nRadius = <\/span><span style=\"color: #B5CEA8\">6371<\/span><span style=\"color: #D4D4D4\">;<\/span><span style=\"color: #6A9955\"> \/\/ Earth&#39;s radius in Kilometers<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\">\/\/ Get the difference between our two points<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\">\/\/ then convert the difference into radians<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">double<\/span><span style=\"color: #D4D4D4\"> nDLat = (nLat2 - nLat1) * (M_PI\/<\/span><span style=\"color: #B5CEA8\">180<\/span><span style=\"color: #D4D4D4\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">double<\/span><span style=\"color: #D4D4D4\"> nDLon = (nLon2 - nLon1) * (M_PI\/<\/span><span style=\"color: #B5CEA8\">180<\/span><span style=\"color: #D4D4D4\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">double<\/span><span style=\"color: #D4D4D4\"> nA = <\/span><span style=\"color: #DCDCAA\">pow<\/span><span style=\"color: #D4D4D4\"> ( <\/span><span style=\"color: #DCDCAA\">sin<\/span><span style=\"color: #D4D4D4\">(nDLat\/<\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">), <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\"> ) + <\/span><span style=\"color: #DCDCAA\">cos<\/span><span style=\"color: #D4D4D4\">(nLat1) * <\/span><span style=\"color: #DCDCAA\">cos<\/span><span style=\"color: #D4D4D4\">(nLat2) * <\/span><span style=\"color: #DCDCAA\">pow<\/span><span style=\"color: #D4D4D4\"> ( <\/span><span style=\"color: #DCDCAA\">sin<\/span><span style=\"color: #D4D4D4\">(nDLon\/<\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">), <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\"> );<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">double<\/span><span style=\"color: #D4D4D4\"> nC = <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\"> * <\/span><span style=\"color: #DCDCAA\">atan2<\/span><span style=\"color: #D4D4D4\">( <\/span><span style=\"color: #DCDCAA\">sqrt<\/span><span style=\"color: #D4D4D4\">(nA), <\/span><span style=\"color: #DCDCAA\">sqrt<\/span><span style=\"color: #D4D4D4\">( <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\"> - nA ));<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">double<\/span><span style=\"color: #D4D4D4\"> nD = nRadius * nC;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> nD;<\/span><span style=\"color: #6A9955\"> \/\/ Return our calculated distance<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Check out the application and you will see that it does NOT do the conversion properly. Here is where I had to do some digging around to see what was happening. The code on the website said one thing but the JavaScript code implemented on the website said another thing! I found that there were two lines omitted from the webpage: lat1 = lat1.toRad(), lat2 = lat2.toRad();. Of course, I had to add this to my code:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 0 16px;font-size:0.8em;width:100%;text-align:left;background-color:#1E1E1E;font-style:italic;color:#D4D4D4\"><span style=\"border-bottom:1px solid rgba(234, 191, 191, 0.2)\">C<\/span><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>double CalculateDistance( double nLat1, double nLon1, double nLat2, double nLon2 )\n{\n    double nRadius = 6371; \/\/ Earth's radius in Kilometers\n    \/\/ Get the difference between our two points\n    \/\/ then convert the difference into radians\n\n    double nDLat = ToRad(nLat2 - nLat1);\n    double nDLon = ToRad(nLon2 - nLon1);\n\n    \/\/ Here is the new line\n    nLat1 =\u00a0 ToRad(nLat1);\n    nLat2 =\u00a0 ToRad(nLat2);\n\n    double nA = pow ( sin(nDLat\/2), 2 ) + cos(nLat1) * cos(nLat2) * pow ( sin(nDLon\/2), 2 );\n\n    double nC = 2 * atan2( sqrt(nA), sqrt( 1 - nA ));\n    double nD = nRadius * nC;\n\n    return nD; \/\/ Return our calculated distance\n}<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">double<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">CalculateDistance<\/span><span style=\"color: #D4D4D4\">( <\/span><span style=\"color: #569CD6\">double<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">nLat1<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #569CD6\">double<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">nLon1<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #569CD6\">double<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">nLat2<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #569CD6\">double<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">nLon2<\/span><span style=\"color: #D4D4D4\"> )<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">double<\/span><span style=\"color: #D4D4D4\"> nRadius = <\/span><span style=\"color: #B5CEA8\">6371<\/span><span style=\"color: #D4D4D4\">;<\/span><span style=\"color: #6A9955\"> \/\/ Earth&#39;s radius in Kilometers<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\">\/\/ Get the difference between our two points<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\">\/\/ then convert the difference into radians<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">double<\/span><span style=\"color: #D4D4D4\"> nDLat = <\/span><span style=\"color: #DCDCAA\">ToRad<\/span><span style=\"color: #D4D4D4\">(nLat2 - nLat1);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">double<\/span><span style=\"color: #D4D4D4\"> nDLon = <\/span><span style=\"color: #DCDCAA\">ToRad<\/span><span style=\"color: #D4D4D4\">(nLon2 - nLon1);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\">\/\/ Here is the new line<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    nLat1 =\u00a0 <\/span><span style=\"color: #DCDCAA\">ToRad<\/span><span style=\"color: #D4D4D4\">(nLat1);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    nLat2 =\u00a0 <\/span><span style=\"color: #DCDCAA\">ToRad<\/span><span style=\"color: #D4D4D4\">(nLat2);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">double<\/span><span style=\"color: #D4D4D4\"> nA = <\/span><span style=\"color: #DCDCAA\">pow<\/span><span style=\"color: #D4D4D4\"> ( <\/span><span style=\"color: #DCDCAA\">sin<\/span><span style=\"color: #D4D4D4\">(nDLat\/<\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">), <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\"> ) + <\/span><span style=\"color: #DCDCAA\">cos<\/span><span style=\"color: #D4D4D4\">(nLat1) * <\/span><span style=\"color: #DCDCAA\">cos<\/span><span style=\"color: #D4D4D4\">(nLat2) * <\/span><span style=\"color: #DCDCAA\">pow<\/span><span style=\"color: #D4D4D4\"> ( <\/span><span style=\"color: #DCDCAA\">sin<\/span><span style=\"color: #D4D4D4\">(nDLon\/<\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">), <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\"> );<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">double<\/span><span style=\"color: #D4D4D4\"> nC = <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\"> * <\/span><span style=\"color: #DCDCAA\">atan2<\/span><span style=\"color: #D4D4D4\">( <\/span><span style=\"color: #DCDCAA\">sqrt<\/span><span style=\"color: #D4D4D4\">(nA), <\/span><span style=\"color: #DCDCAA\">sqrt<\/span><span style=\"color: #D4D4D4\">( <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\"> - nA ));<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">double<\/span><span style=\"color: #D4D4D4\"> nD = nRadius * nC;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> nD;<\/span><span style=\"color: #6A9955\"> \/\/ Return our calculated distance<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Anyone who is looking the JavaScript code will wonder where to get the toRad function. This was actually declared within&nbsp; the webpage, and I implemented my own C version called ToRad.<\/p>\n\n\n\n<p>Now let\u2019s move onto incorporating Zip codes into our example!<\/p>\n\n\n\n<p><strong>Creating code that can handle Zip codes<\/strong><\/p>\n\n\n\n<p>Now, to be able to use Zip codes in our example means that we have to be able to map our zip code to a Latitude\/Longitude pair. Fortunately for us, there is a complete listing of postal codes at http:\/\/sourceforge.net\/projects\/zips\/ , complete with town, state and postal code. All we have to do, is to read the values into our application and then create a routine for getting our Latitude\/Longitude with a given Zip code.<\/p>\n\n\n\n<p>For those who don\u2019t know, Zip is actually an acronym for Zoning Improvement Plan. If you do a little research, you will find that there is an interesting history behind this 5 digit number!<\/p>\n\n\n\n<p>In order for us to use our text file, there are a couple of things we have to do:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Read the text file contents into memory.<\/li>\n\n\n\n<li>Parse the file contents into a container variable.<\/li>\n\n\n\n<li>Create a function that allows us to easy obtain the Latitude and Longitude for a given Zip code.<\/li>\n<\/ol>\n\n\n\n<p>Since the above listed steps can be accomplished using cross platform friendly POSIX code, I will implement the C code in a C++ file to keep things nice and neat. The reason for putting my code in a C++ file is because I wanted to create a C++ struct that would hold my Latitude Longitude pair. Using a C++ struct allows me to create private and public struct members and even a constructor.<\/p>\n\n\n\n<p><strong>Create a routine to check the validity of the output<\/strong><\/p>\n\n\n\n<p>This part was pretty easy. Since the JavaScript implementation works, I created a simpler version of the JavaScript code and posted that page at the end of this article. Using that code, and Google, I can see that the application is outputting the right distance.<\/p>\n\n\n\n<p>I hoped you enjoyed reading this article as much as I did writing it \ud83d\ude09 Check out the sample code and happy coding!<\/p>\n\n\n\n<p><a title=\"Haversine application for Mac OS X\" href=\"https:\/\/jaimerios.com\/wp-content\/uploads\/2008\/02\/haversine.zip\">Haversine application for Mac OS X<\/a><\/p>\n\n\n\n<p>&nbsp;<\/p>\n\n\n\n<p><a title=\"JavaScript Implementation of Haversine Formula\" href=\"https:\/\/jaimerios.com\/wp-content\/uploads\/2008\/02\/harversineformulahtml.zip\">JavaScript Implementation of Haversine Formula<\/a><\/p>\n\n\n\n<p><a title=\"C Source Code and Xcode Project\" href=\"https:\/\/jaimerios.com\/wp-content\/uploads\/2008\/02\/haversinesrc.zip\">C Source Code and Xcode Project<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>2016 UpdateThe code and the Xcode project for this article is now also available on GitHub. A while ago, I worked on a project where I had to implement code in C\/C++ that could calculate the distance between 2 locations within the United States. It would be nice if the Earth was flat, since this &#8230; <a title=\"Calculating distance between 2 points on Earth using the Haversine formula\" class=\"read-more\" href=\"https:\/\/jaimerios.com\/?p=39\" aria-label=\"Read more about Calculating distance between 2 points on Earth using the Haversine formula\">Read more<\/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":[100,226,264],"class_list":["post-39","post","type-post","status-publish","format-standard","hentry","category-coding","tag-c","tag-haversine","tag-javascript"],"_links":{"self":[{"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/posts\/39","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jaimerios.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=39"}],"version-history":[{"count":2,"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/posts\/39\/revisions"}],"predecessor-version":[{"id":1903,"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/posts\/39\/revisions\/1903"}],"wp:attachment":[{"href":"https:\/\/jaimerios.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=39"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jaimerios.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=39"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jaimerios.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=39"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}