Last active
September 1, 2022 15:48
-
-
Save devrmartins/a6136c27c54d5b8b906d8e72f8aa124e to your computer and use it in GitHub Desktop.
Obter distância utilizando Latitude e Longitude
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static double getDistanciaByLatLong(Localizacao usuario, Localizacao ordem) { | |
| /* Agradecimento ao Thiago Galbiatti Vespa | |
| * https://thiagovespa.com.br/blog/2010/09/10/distancia-utilizando-coordenadas-geograficas-em-java | |
| */ | |
| int EARTH_RADIUS_KM = 6371; | |
| double firstLatToRad = Math.toRadians(usuario.getLatitude()); | |
| double secondLatToRad = Math.toRadians(ordem.getLatitude()); | |
| double deltaLongitudeInRad = Math.toRadians(ordem.getLongitude() - usuario.getLongitude()); | |
| return Math.acos(Math.cos(firstLatToRad) * Math.cos(secondLatToRad) | |
| * Math.cos(deltaLongitudeInRad) + Math.sin(firstLatToRad) | |
| * Math.sin(secondLatToRad)) | |
| * EARTH_RADIUS_KM; | |
| } | |
| public static String getDistanciaView(Localizacao usuario, Localizacao ordem) { | |
| double distancia = getDistanciaByLatLong(usuario, ordem); | |
| String medida; | |
| double valor; | |
| if (distancia > 0.99) { | |
| medida = "km"; | |
| valor = round(distancia, 2); | |
| } else { | |
| medida = "m"; | |
| valor = round((distancia * 1000), 2); | |
| } | |
| return String.format("Distância: %s %s", valor, medida); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Localizacao implements Serializable { | |
| private Double latitude; | |
| private Double longitude; | |
| public Localizacao(Double latitude, Double longitude) { | |
| this.latitude = latitude; | |
| this.longitude = longitude; | |
| } | |
| public Localizacao() { | |
| } | |
| public Double getLatitude() { | |
| return latitude; | |
| } | |
| public void setLatitude(Double latitude) { | |
| this.latitude = latitude; | |
| } | |
| public Double getLongitude() { | |
| return longitude; | |
| } | |
| public void setLongitude(Double longitude) { | |
| this.longitude = longitude; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment