Here you can find the source of distanceInMeters(double lat1, double lon1, double lat2, double lon2)
Parameter | Description |
---|---|
lat1 | latitude of point 1 |
lon1 | longitude of point 1 |
lat2 | latitude of point 2 |
lon2 | longitude of point 2 |
static double distanceInMeters(double lat1, double lon1, double lat2, double lon2)
//package com.java2s; /* MOD_V2.0/*from ww w. ja v a 2 s . co m*/ * Copyright (c) 2012 OpenDA Association * All rights reserved. * * This file is part of OpenDA. * * OpenDA is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation, either version 3 of * the License, or (at your option) any later version. * * OpenDA 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with OpenDA. If not, see <http://www.gnu.org/licenses/>. */ public class Main { private static final double METERS_PER_DEGREE = 60. * 1852.27; /** * This calculation is not the most accurate, but it takes less computation time. * * @param lat1 latitude of point 1 * @param lon1 longitude of point 1 * @param lat2 latitude of point 2 * @param lon2 longitude of point 2 * @return distance in meters */ static double distanceInMeters(double lat1, double lon1, double lat2, double lon2) { //this calculation is not the most accurate, but it takes less computation time. double dy = (lat2 - lat1) * METERS_PER_DEGREE; double phi = (lat2 + lat1) * 0.017453 / 2; double dx = (lon2 - lon1) * METERS_PER_DEGREE * Math.cos(phi); return Math.hypot(dx, dy); } }