Java tutorial
//package com.java2s; public class Main { private final static double EARTH_RADIUS = 6378137.0; public static double gps2km(final double lat_a, final double lng_a, final double lat_b, final double lng_b) { if (!isGpsValid(lng_a, lat_a) || !isGpsValid(lng_b, lat_b)) { return -1; } final double radLat1 = (lat_a * Math.PI / 180.0); final double radLat2 = (lat_b * Math.PI / 180.0); final double a = radLat1 - radLat2; final double b = (lng_a - lng_b) * Math.PI / 180.0; final double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2))); return s * EARTH_RADIUS; } public static boolean isGpsValid(final double longitude, final double latitude) { return Math.abs(latitude) > 0.001 && Math.abs(longitude) > 0.001; } }