{"id":666,"date":"2021-01-05T00:00:00","date_gmt":"2021-01-05T00:00:00","guid":{"rendered":"https:\/\/wp3.geodatasource.com\/tutorials\/how-to-calculate-the-distance-between-2-locations-using-c\/"},"modified":"2021-01-05T00:00:00","modified_gmt":"2021-01-05T00:00:00","password":"","slug":"how-to-calculate-the-distance-between-2-locations-using-c","status":"publish","type":"docs","link":"https:\/\/www.geodatasource.com\/resources\/tutorials\/how-to-calculate-the-distance-between-2-locations-using-c\/","title":{"rendered":"How to calculate the distance between 2 locations using C#"},"content":{"rendered":"\n<p>In this tutorial, we will show you how to calculate the distance between two locations geolocated by using latitude and longitude in C#. This distance calculation uses the <strong>Spherical Law of Cosines<\/strong>, which uses trigonometry to measure the curvature of the earth, to accurately measure the distances on the Earth.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\nprivate double distance(double lat1, double lon1, double lat2, double lon2, char unit) {\n  if ((lat1 == lat2) &amp;&amp; (lon1 == lon2)) {\n    return 0;\n  }\n  else {\n    double theta = lon1 - lon2;\n    double dist = Math.Sin(deg2rad(lat1)) * Math.Sin(deg2rad(lat2)) + Math.Cos(deg2rad(lat1)) * Math.Cos(deg2rad(lat2)) * Math.Cos(deg2rad(theta));\n    dist = Math.Acos(dist);\n    dist = rad2deg(dist);\n    dist = dist * 60 * 1.1515;\n    if (unit == 'K') {\n      dist = dist * 1.609344;\n    } else if (unit == 'N') {\n      dist = dist * 0.8684;\n    }\n    return (dist);\n  }\n}\n\n\/\/:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::\n\/\/::  This function converts decimal degrees to radians             :::\n\/\/:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::\nprivate double deg2rad(double deg) {\n  return (deg * Math.PI \/ 180.0);\n}\n\n\/\/:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::\n\/\/::  This function converts radians to decimal degrees             :::\n\/\/:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::\nprivate double rad2deg(double rad) {\n  return (rad \/ Math.PI * 180.0);\n}\n\nConsole.WriteLine(distance(32.9697, -96.80322, 29.46786, -98.53506, \"M\"));\nConsole.WriteLine(distance(32.9697, -96.80322, 29.46786, -98.53506, \"K\"));\nConsole.WriteLine(distance(32.9697, -96.80322, 29.46786, -98.53506, \"N\"));\n<\/code><\/pre>\n\n\n\n<p>The code above creates the function named <strong>distance <\/strong>to calculate the distance between two locations. It implies the simple spherical law of cosines that gives well-conditioned results down to distances as small as a few meters on the Earth\u2019s surface. The distance function makes use of the spherical law of cosines formula <code>cos <em>c<\/em> = cos <em>a<\/em> cos <em>b<\/em> + sin <em>a<\/em> sin <em>b<\/em> cos <em>C<\/em><\/code> and derived into the distance calculation.<\/p>\n\n\n\n<p>Parameters that are passed to the distance function are:<br><strong>lat1<\/strong>, <strong>lon1<\/strong> = Latitude and Longitude of point 1 in decimal degrees<br><strong>lat2<\/strong>, <strong>lon2 <\/strong>= Latitude and Longitude of point 2 in decimal degrees<br><strong>unit <\/strong>= the unit you desire for results where \u2018M\u2019 is the statute miles (default), \u2018K\u2019 is kilometers and \u2018N\u2019 is nautical miles<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will show you how to calculate the distance between two locations geolocated by using latitude and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","template":"","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"doc_category":[21],"doc_tag":[],"class_list":["post-666","docs","type-docs","status-publish","hentry","doc_category-technical"],"year_month":"2026-06","word_count":301,"total_views":0,"reactions":{"happy":0,"normal":0,"sad":0},"author_info":{"name":"hexasoft","author_nicename":"hexasoft","author_url":"https:\/\/www.geodatasource.com\/resources\/author\/hexasoft\/"},"doc_category_info":[{"term_name":"Technical","term_url":"https:\/\/www.geodatasource.com\/resources\/categories\/technical\/"}],"doc_tag_info":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to calculate the distance between 2 locations using C# |<\/title>\n<meta name=\"description\" content=\"This article shows the How to calculate the distance between 2 locations using C#. Read about it to learn more details.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.geodatasource.com\/resources\/tutorials\/how-to-calculate-the-distance-between-2-locations-using-c\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to calculate the distance between 2 locations using C# |\" \/>\n<meta property=\"og:description\" content=\"This article shows the How to calculate the distance between 2 locations using C#. Read about it to learn more details.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.geodatasource.com\/resources\/tutorials\/how-to-calculate-the-distance-between-2-locations-using-c\/\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/geodatasource\/\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.geodatasource.com\/resources\/wp-content\/uploads\/2021\/05\/banner-articles-and-tutorials.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"600\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@_geodatasource\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.geodatasource.com\\\/resources\\\/tutorials\\\/how-to-calculate-the-distance-between-2-locations-using-c\\\/\",\"url\":\"https:\\\/\\\/www.geodatasource.com\\\/resources\\\/tutorials\\\/how-to-calculate-the-distance-between-2-locations-using-c\\\/\",\"name\":\"How to calculate the distance between 2 locations using C# |\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.geodatasource.com\\\/resources\\\/#website\"},\"datePublished\":\"2021-01-05T00:00:00+00:00\",\"description\":\"This article shows the How to calculate the distance between 2 locations using C#. Read about it to learn more details.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.geodatasource.com\\\/resources\\\/tutorials\\\/how-to-calculate-the-distance-between-2-locations-using-c\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.geodatasource.com\\\/resources\\\/tutorials\\\/how-to-calculate-the-distance-between-2-locations-using-c\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.geodatasource.com\\\/resources\\\/tutorials\\\/how-to-calculate-the-distance-between-2-locations-using-c\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.geodatasource.com\\\/resources\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Tutorials\",\"item\":\"https:\\\/\\\/www.geodatasource.com\\\/resources\\\/tutorials\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"How to calculate the distance between 2 locations using C#\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.geodatasource.com\\\/resources\\\/#website\",\"url\":\"https:\\\/\\\/www.geodatasource.com\\\/resources\\\/\",\"name\":\"GeoDataSource Articles & Tutorials\",\"description\":\"Resources About GeoDataSource Services\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.geodatasource.com\\\/resources\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.geodatasource.com\\\/resources\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.geodatasource.com\\\/resources\\\/#organization\",\"name\":\"GeoDataSource.com\",\"url\":\"https:\\\/\\\/www.geodatasource.com\\\/resources\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.geodatasource.com\\\/resources\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.geodatasource.com\\\/resources\\\/wp-content\\\/uploads\\\/2019\\\/07\\\/logo-e1564122827936.png\",\"contentUrl\":\"https:\\\/\\\/www.geodatasource.com\\\/resources\\\/wp-content\\\/uploads\\\/2019\\\/07\\\/logo-e1564122827936.png\",\"width\":220,\"height\":27,\"caption\":\"GeoDataSource.com\"},\"image\":{\"@id\":\"https:\\\/\\\/www.geodatasource.com\\\/resources\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/geodatasource\\\/\",\"https:\\\/\\\/x.com\\\/_geodatasource\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to calculate the distance between 2 locations using C# |","description":"This article shows the How to calculate the distance between 2 locations using C#. Read about it to learn more details.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.geodatasource.com\/resources\/tutorials\/how-to-calculate-the-distance-between-2-locations-using-c\/","og_locale":"en_US","og_type":"article","og_title":"How to calculate the distance between 2 locations using C# |","og_description":"This article shows the How to calculate the distance between 2 locations using C#. Read about it to learn more details.","og_url":"https:\/\/www.geodatasource.com\/resources\/tutorials\/how-to-calculate-the-distance-between-2-locations-using-c\/","article_publisher":"https:\/\/www.facebook.com\/geodatasource\/","og_image":[{"width":1200,"height":600,"url":"https:\/\/www.geodatasource.com\/resources\/wp-content\/uploads\/2021\/05\/banner-articles-and-tutorials.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_site":"@_geodatasource","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.geodatasource.com\/resources\/tutorials\/how-to-calculate-the-distance-between-2-locations-using-c\/","url":"https:\/\/www.geodatasource.com\/resources\/tutorials\/how-to-calculate-the-distance-between-2-locations-using-c\/","name":"How to calculate the distance between 2 locations using C# |","isPartOf":{"@id":"https:\/\/www.geodatasource.com\/resources\/#website"},"datePublished":"2021-01-05T00:00:00+00:00","description":"This article shows the How to calculate the distance between 2 locations using C#. Read about it to learn more details.","breadcrumb":{"@id":"https:\/\/www.geodatasource.com\/resources\/tutorials\/how-to-calculate-the-distance-between-2-locations-using-c\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.geodatasource.com\/resources\/tutorials\/how-to-calculate-the-distance-between-2-locations-using-c\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.geodatasource.com\/resources\/tutorials\/how-to-calculate-the-distance-between-2-locations-using-c\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.geodatasource.com\/resources\/"},{"@type":"ListItem","position":2,"name":"Tutorials","item":"https:\/\/www.geodatasource.com\/resources\/tutorials\/"},{"@type":"ListItem","position":3,"name":"How to calculate the distance between 2 locations using C#"}]},{"@type":"WebSite","@id":"https:\/\/www.geodatasource.com\/resources\/#website","url":"https:\/\/www.geodatasource.com\/resources\/","name":"GeoDataSource Articles & Tutorials","description":"Resources About GeoDataSource Services","publisher":{"@id":"https:\/\/www.geodatasource.com\/resources\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.geodatasource.com\/resources\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.geodatasource.com\/resources\/#organization","name":"GeoDataSource.com","url":"https:\/\/www.geodatasource.com\/resources\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.geodatasource.com\/resources\/#\/schema\/logo\/image\/","url":"https:\/\/www.geodatasource.com\/resources\/wp-content\/uploads\/2019\/07\/logo-e1564122827936.png","contentUrl":"https:\/\/www.geodatasource.com\/resources\/wp-content\/uploads\/2019\/07\/logo-e1564122827936.png","width":220,"height":27,"caption":"GeoDataSource.com"},"image":{"@id":"https:\/\/www.geodatasource.com\/resources\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/geodatasource\/","https:\/\/x.com\/_geodatasource"]}]}},"_links":{"self":[{"href":"https:\/\/www.geodatasource.com\/resources\/wp-json\/wp\/v2\/docs\/666","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.geodatasource.com\/resources\/wp-json\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/www.geodatasource.com\/resources\/wp-json\/wp\/v2\/types\/docs"}],"author":[{"embeddable":true,"href":"https:\/\/www.geodatasource.com\/resources\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.geodatasource.com\/resources\/wp-json\/wp\/v2\/comments?post=666"}],"version-history":[{"count":0,"href":"https:\/\/www.geodatasource.com\/resources\/wp-json\/wp\/v2\/docs\/666\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.geodatasource.com\/resources\/wp-json\/wp\/v2\/media?parent=666"}],"wp:term":[{"taxonomy":"doc_category","embeddable":true,"href":"https:\/\/www.geodatasource.com\/resources\/wp-json\/wp\/v2\/doc_category?post=666"},{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/www.geodatasource.com\/resources\/wp-json\/wp\/v2\/doc_tag?post=666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}