publicstatic List<LatLng>simplify(List<LatLng> list,double tolerance){int index =0;double dmax =0;int lastIndex = list.size()-1;// Find the point with the maximum distancefor(int i =1; i < lastIndex; i++){double d =pointToLineDistance(list.get(0), list.get(lastIndex), list.get(i));if(d > dmax){
index = i;
dmax = d;}}// If max distance is greater than epsilon, recursively simplify
List<LatLong> ResultList =newArrayList<LatLong>();if(dmax > tolerance){// Recursive call
List<LatLong> recResults1 =simplify(list.subList(0, index +1), tolerance);
List<LatLong> recResults2 =simplify(list.subList(index, lastIndex +1), tolerance);// Build the result list
recResults1.remove(recResults1.size()-1);
ResultList.addAll(recResults1);
ResultList.addAll(recResults2);}else{
ResultList.add(list.get(0));
ResultList.add(list.get(lastIndex));}// Return the resultreturn ResultList;}
/**
* 提供从点P到穿过a - B的线段的距离。如果点不在直线的一边,则返回到最近点的距离
* Provides the distance from a point P to the line segment that passes
* distance to the closest point
*
* @param p1 First point of the line
* @param p2 Second point of the line
* @param point Point to measure the distance
* through A-B. If the point is not on the side of the line, returns the
*/publicstaticdoublepointToLineDistance(LatLong p1, LatLong p2, LatLong point){double A = point.getLatitude()- p1.getLatitude();double B = point.getLongitude()- p1.getLongitude();double C = p2.getLatitude()- p1.getLatitude();double D = p2.getLongitude()- p1.getLongitude();double dot = A * C + B * D;double len_sq = C * C + D * D;double param = dot / len_sq;double xx, yy;if(param <0)// point behind the segment{
xx = p1.getLatitude();
yy = p1.getLongitude();}elseif(param >1)// point after the segment{
xx = p2.getLatitude();
yy = p2.getLongitude();}else{// point on the side of the segment
xx = p1.getLatitude()+ param * C;
yy = p1.getLongitude()+ param * D;}return Math.hypot(xx - point.getLatitude(), yy - point.getLongitude());}