Here you can find the source of distance(double lat1, double lon1, double lat2, double lon2)
public static double distance(double lat1, double lon1, double lat2, double lon2)
//package com.java2s; /*/*from w ww .j ava 2s . co m*/ * Mosaic - Location Based Messaging * Copyright (C) 2013 Bryan Emmanuel * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * * Bryan Emmanuel piusvelte@gmail.com */ public class Main { public static final int EARTH_RADIUS = 6378135; public static double distance(double lat1, double lon1, double lat2, double lon2) { double p1lat = Math.toRadians(lat1); double p1lon = Math.toRadians(lon1); double p2lat = Math.toRadians(lat2); double p2lon = Math.toRadians(lon2); return EARTH_RADIUS * Math.acos(makeDoubleInRange( Math.sin(p1lat) * Math.sin(p2lat) + Math.cos(p1lat) * Math.cos(p2lat) * Math.cos(p2lon - p1lon))); } public static double makeDoubleInRange(double d) { if (d > 1) return 1; else if (d < -1) return -1; return d; } }