Created
December 11, 2015 17:01
-
-
Save kuratowsky/5fc2ca03815f255d9de0 to your computer and use it in GitHub Desktop.
Calcular distancia, expresada en KM, entre dos coordenadas GPS
This file contains 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
package com.benetjoandarder.helpers | |
import java.text.DecimalFormat; | |
public class ProjectHelpers { | |
public static double calcDist(double lat1, double lon1, double lat2, double lon2){ | |
DecimalFormat df = new DecimalFormat("#.###"); | |
double R = 6378.137;//Radio de la tierra en km | |
double dLat = Math.toRadians(lat2-lat1); | |
double dLong = Math.toRadians(lon2-lon1); | |
double a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * Math.sin(dLong/2) * Math.sin(dLong/2); | |
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); | |
return R * c; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment