public double distanceToLineSegment(LatLong A,LatLong B){
  double Ax=(A.lng - lng) * Math.cos(0.5 * radians * (A.lat + lat));
  double Ay=(A.lat - lat);
  double Bx=(B.lng - lng) * Math.cos(0.5 * radians * (B.lat + lat));
  double By=(B.lat - lat);
  if (Ay == By) {
    if ((Ax < 0 && Bx < 0) || (Ax > 0 && Bx > 0))     return Math.sqrt(Math.min(Ax * Ax + Ay * Ay,Bx * Bx + By * By)) * EarthRadius * radians;
    if (Ax == Bx)     return Math.sqrt(Ax * Ax + Ay * Ay) * EarthRadius * radians;
  }
 else {
    double Ix=((By - Ay) * (Ax * By - Ay * Bx)) / ((Ax - Bx) * (Ax - Bx) + (Ay - By) * (Ay - By));
    if ((Ix < Ax && Ix < Bx) || (Ix > Ax && Ix > Bx)) {
      return Math.sqrt(Math.min(Ax * Ax + Ay * Ay,Bx * Bx + By * By)) * EarthRadius * radians;
    }
  }
  return Math.abs((Bx - Ax) * Ay - Ax * (By - Ay)) / Math.sqrt((Bx - Ax) * (Bx - Ax) + (By - Ay) * (By - Ay)) * EarthRadius * radians;
}

public double distanceToLineSegment ( LatLong A , LatLong B ) { double Ax = ( A . lng - lng ) * Math . cos ( 0.5 * radians * ( A . lat + lat ) ) ; double Ay = ( A . lat - lat ) ; double Bx = ( B . lng - lng ) * Math . cos ( 0.5 * radians * ( B . lat + lat ) ) ; double By = ( B . lat - lat ) ; if ( Ay == By ) { if ( ( Ax < 0 && Bx < 0 ) || ( Ax > 0 && Bx > 0 ) ) return Math . sqrt ( Math . min ( Ax * Ax + Ay * Ay , Bx * Bx + By * By ) ) * EarthRadius * radians ; if ( Ax == Bx ) return Math . sqrt ( Ax * Ax + Ay * Ay ) * EarthRadius * radians ; } else { double Ix = ( ( By - Ay ) * ( Ax * By - Ay * Bx ) ) / ( ( Ax - Bx ) * ( Ax - Bx ) + ( Ay - By ) * ( Ay - By ) ) ; if ( ( Ix < Ax && Ix < Bx ) || ( Ix > Ax && Ix > Bx ) ) { return Math . sqrt ( Math . min ( Ax * Ax + Ay * Ay , Bx * Bx + By * By ) ) * EarthRadius * radians ; } } return Math . abs ( ( Bx - Ax ) * Ay - Ax * ( By - Ay ) ) / Math . sqrt ( ( Bx - Ax ) * ( Bx - Ax ) + ( By - Ay ) * ( By - Ay ) ) * EarthRadius * radians ; }
this is the equirectangular approximation .
