@Test public void testDistanceToLineLessThanDistanceToExtrems() { LatLng startLine = new LatLng(28.05359, -82.41632); LatLng endLine = new LatLng(28.05310, -82.41634); LatLng p = new LatLng(28.05342, -82.41594); double distance = PolyUtil.distanceToLine(p, startLine, endLine); double distanceToStart = SphericalUtil.computeDistanceBetween(p, startLine); double distanceToEnd = SphericalUtil.computeDistanceBetween(p, endLine); assertTrue("Wrong distance.", distance <= distanceToStart && distance <= distanceToEnd); }
public static double distanceToLine(final LatLng p, final LatLng start, final LatLng end) { if (start.equals(end)) { return computeDistanceBetween(end, p); } final double s0lat = toRadians(p.latitude); final double s0lng = toRadians(p.longitude); final double s1lat = toRadians(start.latitude); final double s1lng = toRadians(start.longitude); final double s2lat = toRadians(end.latitude); final double s2lng = toRadians(end.longitude); double s2s1lat = s2lat - s1lat; double s2s1lng = s2lng - s1lng; final double u = ((s0lat - s1lat) * s2s1lat + (s0lng - s1lng) * s2s1lng) / (s2s1lat * s2s1lat + s2s1lng * s2s1lng); if (u <= 0) { return computeDistanceBetween(p, start); } if (u >= 1) { return computeDistanceBetween(p, end); } LatLng su = new LatLng(start.latitude + u * (end.latitude - start.latitude), start.longitude + u * (end.longitude - start.longitude)); return computeDistanceBetween(p, su); }
PolyUtil { public static double distanceToLine(final LatLng p, final LatLng start, final LatLng end) { if (start.equals(end)) { return computeDistanceBetween(end, p); } final double s0lat = toRadians(p.latitude); final double s0lng = toRadians(p.longitude); final double s1lat = toRadians(start.latitude); final double s1lng = toRadians(start.longitude); final double s2lat = toRadians(end.latitude); final double s2lng = toRadians(end.longitude); double s2s1lat = s2lat - s1lat; double s2s1lng = s2lng - s1lng; final double u = ((s0lat - s1lat) * s2s1lat + (s0lng - s1lng) * s2s1lng) / (s2s1lat * s2s1lat + s2s1lng * s2s1lng); if (u <= 0) { return computeDistanceBetween(p, start); } if (u >= 1) { return computeDistanceBetween(p, end); } LatLng su = new LatLng(start.latitude + u * (end.latitude - start.latitude), start.longitude + u * (end.longitude - start.longitude)); return computeDistanceBetween(p, su); } }
PolyUtil { public static double distanceToLine(final LatLng p, final LatLng start, final LatLng end) { if (start.equals(end)) { return computeDistanceBetween(end, p); } final double s0lat = toRadians(p.latitude); final double s0lng = toRadians(p.longitude); final double s1lat = toRadians(start.latitude); final double s1lng = toRadians(start.longitude); final double s2lat = toRadians(end.latitude); final double s2lng = toRadians(end.longitude); double s2s1lat = s2lat - s1lat; double s2s1lng = s2lng - s1lng; final double u = ((s0lat - s1lat) * s2s1lat + (s0lng - s1lng) * s2s1lng) / (s2s1lat * s2s1lat + s2s1lng * s2s1lng); if (u <= 0) { return computeDistanceBetween(p, start); } if (u >= 1) { return computeDistanceBetween(p, end); } LatLng su = new LatLng(start.latitude + u * (end.latitude - start.latitude), start.longitude + u * (end.longitude - start.longitude)); return computeDistanceBetween(p, su); } private  PolyUtil(); }
PolyUtil { public static double distanceToLine(final LatLng p, final LatLng start, final LatLng end) { if (start.equals(end)) { return computeDistanceBetween(end, p); } final double s0lat = toRadians(p.latitude); final double s0lng = toRadians(p.longitude); final double s1lat = toRadians(start.latitude); final double s1lng = toRadians(start.longitude); final double s2lat = toRadians(end.latitude); final double s2lng = toRadians(end.longitude); double s2s1lat = s2lat - s1lat; double s2s1lng = s2lng - s1lng; final double u = ((s0lat - s1lat) * s2s1lat + (s0lng - s1lng) * s2s1lng) / (s2s1lat * s2s1lat + s2s1lng * s2s1lng); if (u <= 0) { return computeDistanceBetween(p, start); } if (u >= 1) { return computeDistanceBetween(p, end); } LatLng su = new LatLng(start.latitude + u * (end.latitude - start.latitude), start.longitude + u * (end.longitude - start.longitude)); return computeDistanceBetween(p, su); } private  PolyUtil(); static boolean containsLocation(LatLng point, List<LatLng> polygon, boolean geodesic); static boolean containsLocation(double latitude, double longitude, List<LatLng> polygon, boolean geodesic); static boolean isLocationOnEdge(LatLng point, List<LatLng> polygon, boolean geodesic,
                                           double tolerance); static boolean isLocationOnEdge(LatLng point, List<LatLng> polygon, boolean geodesic); static boolean isLocationOnPath(LatLng point, List<LatLng> polyline,
                                           boolean geodesic, double tolerance); static boolean isLocationOnPath(LatLng point, List<LatLng> polyline,
                                           boolean geodesic); static int locationIndexOnPath(LatLng point, List<LatLng> poly,
                                          boolean geodesic, double tolerance); static int locationIndexOnPath(LatLng point, List<LatLng> polyline,
                                          boolean geodesic); static int locationIndexOnEdgeOrPath(LatLng point, List<LatLng> poly, boolean closed,
                                                boolean geodesic, double toleranceEarth); static List<LatLng> simplify(List<LatLng> poly, double tolerance); static boolean isClosedPolygon(List<LatLng> poly); static double distanceToLine(final LatLng p, final LatLng start, final LatLng end); static List<LatLng> decode(final String encodedPath); static String encode(final List<LatLng> path); }
PolyUtil { public static double distanceToLine(final LatLng p, final LatLng start, final LatLng end) { if (start.equals(end)) { return computeDistanceBetween(end, p); } final double s0lat = toRadians(p.latitude); final double s0lng = toRadians(p.longitude); final double s1lat = toRadians(start.latitude); final double s1lng = toRadians(start.longitude); final double s2lat = toRadians(end.latitude); final double s2lng = toRadians(end.longitude); double s2s1lat = s2lat - s1lat; double s2s1lng = s2lng - s1lng; final double u = ((s0lat - s1lat) * s2s1lat + (s0lng - s1lng) * s2s1lng) / (s2s1lat * s2s1lat + s2s1lng * s2s1lng); if (u <= 0) { return computeDistanceBetween(p, start); } if (u >= 1) { return computeDistanceBetween(p, end); } LatLng su = new LatLng(start.latitude + u * (end.latitude - start.latitude), start.longitude + u * (end.longitude - start.longitude)); return computeDistanceBetween(p, su); } private  PolyUtil(); static boolean containsLocation(LatLng point, List<LatLng> polygon, boolean geodesic); static boolean containsLocation(double latitude, double longitude, List<LatLng> polygon, boolean geodesic); static boolean isLocationOnEdge(LatLng point, List<LatLng> polygon, boolean geodesic,
                                           double tolerance); static boolean isLocationOnEdge(LatLng point, List<LatLng> polygon, boolean geodesic); static boolean isLocationOnPath(LatLng point, List<LatLng> polyline,
                                           boolean geodesic, double tolerance); static boolean isLocationOnPath(LatLng point, List<LatLng> polyline,
                                           boolean geodesic); static int locationIndexOnPath(LatLng point, List<LatLng> poly,
                                          boolean geodesic, double tolerance); static int locationIndexOnPath(LatLng point, List<LatLng> polyline,
                                          boolean geodesic); static int locationIndexOnEdgeOrPath(LatLng point, List<LatLng> poly, boolean closed,
                                                boolean geodesic, double toleranceEarth); static List<LatLng> simplify(List<LatLng> poly, double tolerance); static boolean isClosedPolygon(List<LatLng> poly); static double distanceToLine(final LatLng p, final LatLng start, final LatLng end); static List<LatLng> decode(final String encodedPath); static String encode(final List<LatLng> path); static final double DEFAULT_TOLERANCE; }