Here you can find the source of distFrom(double lat1, double lng1, double lat2, double lng2)
Parameter | Description |
---|---|
lat1 | a parameter |
lng1 | a parameter |
lat2 | a parameter |
lng2 | a parameter |
public static double distFrom(double lat1, double lng1, double lat2, double lng2)
//package com.java2s; //License from project: Apache License public class Main { /**//from w w w.ja va2 s. co m * Returns the distance between 2 points in meters * * @param lat1 * @param lng1 * @param lat2 * @param lng2 * @return meters */ public static double distFrom(double lat1, double lng1, double lat2, double lng2) { double earthRadius = 3958.75; double dLat = Math.toRadians(lat2 - lat1); double dLng = Math.toRadians(lng2 - lng1); double sindLat = Math.sin(dLat / 2); double sindLng = Math.sin(dLng / 2); double a = Math.pow(sindLat, 2) + Math.pow(sindLng, 2) * Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)); double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); double dist = earthRadius * c; int meterConversion = 1609; float totalMeters = (float) (dist * meterConversion); return totalMeters; } }