{"id":37694,"date":"2008-11-25T23:37:30","date_gmt":"2008-11-25T23:37:30","guid":{"rendered":"https:\/\/www.viafirma.com\/haversine-formula-implementation-in-java\/"},"modified":"2026-01-23T11:22:32","modified_gmt":"2026-01-23T11:22:32","slug":"haversine-formula-implementation-in-java","status":"publish","type":"post","link":"https:\/\/www.viafirma.com\/en\/haversine-formula-implementation-in-java\/","title":{"rendered":"Haversine formula implementation in Java"},"content":{"rendered":"\r\n<p>The <a href=\"https:\/\/en.wikipedia.org\/wiki\/Haversine_formula\" target=\"_blank\" rel=\"noopener\">Haversine formula<\/a> is a mathematical tool used to calculate the distance between two points on the surface of a sphere, such as the Earth, whose longitude and latitude are known. This method is essential in geolocation and GPS navigation applications. In this <a href=\"https:\/\/www.viafirma.com\/en\/\">Viafirma<\/a> article, we will explore how to implement this formula in Java, providing practical examples and ready-to-use code.<\/p>\r\n<h2>How to implement the Haversine Formula in Java<\/h2>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code><em>private static int calculateDistanceByHaversineFormula(double lon1, double lat1, double lon2, double lat2) {<\/em>\r\ndouble earthRadius = 6371; \/\/ km\r\n\r\ns(lat1);\r\nlon1 = Math.toRadians(lon1);\r\nlat2 = Math.toRadians(lat2);\r\nlon2 = Math.toRadians(lon2);\r\n\r\ndouble dlon = (lon2 - lon1);\r\ndouble dlat = (lat2 - lat1);\r\n\r\ndouble sinlat = Math.sin(dlat \/ 2);\r\ndouble sinlon = Math.sin(dlon \/ 2);\r\n\r\ndouble a = (sinlat * sinlat) + Math.cos(lat1)*Math.cos(lat2)*(sinlon*sinlon);\r\ndouble c = 2 * Math.asin (Math.min(1.0, Math.sqrt(a)));\r\n\r\ndouble distanceInMeters = earthRadius * c * 1000;\r\nreturn (int) distanceInMeters;\r\n}<\/code><\/pre>\r\n\r\n\r\n\r\n<p>In our case it has been used to evaluate the distance to a given establishment from our current position (calculated by GPS).<\/p>\r\n<p><span style=\"font-weight: 400;\"><div class=\"vf_related_posts_wrapper\"><h2 class=\"vf_related_posts_title\">Informaci\u00f3n relacionada<\/h2><div class=\"vf_related_posts\"><article class=\"vc_gitem-post-data-source-post\"><a href=\"https:\/\/www.viafirma.com\/en\/5-ways-to-protect-your-digital-identity\/\"><div class=\"vc_gitem-post-image\"><img loading=\"lazy\" decoding=\"async\" width=\"768\" height=\"576\" src=\"https:\/\/www.viafirma.com\/wp-content\/uploads\/2019\/10\/5-formas-de-proteger-tu-identidad-digital-1-768x576.webp\" class=\"attachment-medium_large size-medium_large wp-post-image\" alt=\"5-formas-de-proteger-tu-identidad-digital\" srcset=\"https:\/\/www.viafirma.com\/wp-content\/uploads\/2019\/10\/5-formas-de-proteger-tu-identidad-digital-1-768x576.webp 768w, https:\/\/www.viafirma.com\/wp-content\/uploads\/2019\/10\/5-formas-de-proteger-tu-identidad-digital-1-300x225.webp 300w, https:\/\/www.viafirma.com\/wp-content\/uploads\/2019\/10\/5-formas-de-proteger-tu-identidad-digital-1.webp 1024w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><\/div><\/a><div class=\"vc_gitem-post-data\"><h3 class=\"vc_gitem-post-data-source-post_title\"><a href=\"https:\/\/www.viafirma.com\/en\/5-ways-to-protect-your-digital-identity\/\">5 ways to protect your digital identity<\/a><\/h3><p>Protecting digital identity is a necessity that an increasing number<\/p><\/div><\/article><article class=\"vc_gitem-post-data-source-post\"><a href=\"https:\/\/www.viafirma.com\/en\/basic-tips-cybersecurity-company\/\"><div class=\"vc_gitem-post-image\"><img loading=\"lazy\" decoding=\"async\" width=\"768\" height=\"475\" src=\"https:\/\/www.viafirma.com\/wp-content\/uploads\/2018\/08\/blog_consejos_ciber-768x475.webp\" class=\"attachment-medium_large size-medium_large wp-post-image\" alt=\"imagen de ordenador con candados ciberseguridad\" srcset=\"https:\/\/www.viafirma.com\/wp-content\/uploads\/2018\/08\/blog_consejos_ciber-768x475.webp 768w, https:\/\/www.viafirma.com\/wp-content\/uploads\/2018\/08\/blog_consejos_ciber-300x186.webp 300w, https:\/\/www.viafirma.com\/wp-content\/uploads\/2018\/08\/blog_consejos_ciber-1024x634.webp 1024w, https:\/\/www.viafirma.com\/wp-content\/uploads\/2018\/08\/blog_consejos_ciber.webp 1228w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><\/div><\/a><div class=\"vc_gitem-post-data\"><h3 class=\"vc_gitem-post-data-source-post_title\"><a href=\"https:\/\/www.viafirma.com\/en\/basic-tips-cybersecurity-company\/\">5 basic tips to improve cybersecurity in your company<\/a><\/h3><p>Cybersecurity is, today, one of the main aspects that companies<\/p><\/div><\/article><\/div><\/div><\/span><\/p>\r\n","protected":false},"excerpt":{"rendered":"Implement the Haversine formula in Java...","protected":false},"author":1,"featured_media":35653,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"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":"default","adv-header-id-meta":"","stick-header-meta":"default","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-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":"","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-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":"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":""},"mobile":{"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":""}},"footnotes":""},"categories":[157],"class_list":["post-37694","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-others"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.viafirma.com\/en\/wp-json\/wp\/v2\/posts\/37694","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.viafirma.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.viafirma.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.viafirma.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.viafirma.com\/en\/wp-json\/wp\/v2\/comments?post=37694"}],"version-history":[{"count":1,"href":"https:\/\/www.viafirma.com\/en\/wp-json\/wp\/v2\/posts\/37694\/revisions"}],"predecessor-version":[{"id":97641,"href":"https:\/\/www.viafirma.com\/en\/wp-json\/wp\/v2\/posts\/37694\/revisions\/97641"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.viafirma.com\/en\/wp-json\/wp\/v2\/media\/35653"}],"wp:attachment":[{"href":"https:\/\/www.viafirma.com\/en\/wp-json\/wp\/v2\/media?parent=37694"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.viafirma.com\/en\/wp-json\/wp\/v2\/categories?post=37694"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}