Answers for "calculate distance between two points"

2

how to calculate distance between two points in javascript

function distance(x1, y1, x2, y2) {
return Math.hypot(x2-x1, y2-y1)
}
Posted by: Guest on August-30-2020
-1

calculate distance between two coordinates

static double coordinateDistance(
      double lat1, double lng1, double lat2, double lng2) {
    const p = 0.017453292519943295;
    const c = cos;
    final a = 0.5 -
        c((lat2 - lat1) * p) / 2 +
        c(lat1 * p) * c(lat2 * p) * (1 - c((lng2 - lng1) * p)) / 2;
    return 12742 * asin(sqrt(a));
  }

  // calculate distance from polyline-coordinates (list of locations)
  static double calculateDistance(
      {@required List<Location> polylineCoordinates}) {
    double _totalDistance = 0.0;

    for (int i = 0; i < polylineCoordinates.length - 1; i++) {
      _totalDistance += coordinateDistance(
        polylineCoordinates[i].lat,
        polylineCoordinates[i].lng,
        polylineCoordinates[i + 1].lat,
        polylineCoordinates[i + 1].lng,
      );
    }

    // distance in KM
    return _totalDistance;
  }
Posted by: Guest on August-25-2021

Code answers related to "calculate distance between two points"

Code answers related to "Javascript"

Browse Popular Code Answers by Language