PolylineUtils { @NonNull public static List<Point> decode(@NonNull final String encodedPath, int precision) { int len = encodedPath.length(); double factor = Math.pow(10, precision); final List<Point> path = new ArrayList<>(); int index = 0; int lat = 0; int lng = 0; while (index < len) { int result = 1; int shift = 0; int temp; do { temp = encodedPath.charAt(index++) - 63 - 1; result += temp << shift; shift += 5; } while (temp >= 0x1f); lat += (result & 1) != 0 ? ~(result >> 1) : (result >> 1); result = 1; shift = 0; do { temp = encodedPath.charAt(index++) - 63 - 1; result += temp << shift; shift += 5; } while (temp >= 0x1f); lng += (result & 1) != 0 ? ~(result >> 1) : (result >> 1); path.add(Point.fromLngLat(lng / factor, lat / factor)); } return path; } private  PolylineUtils(); @NonNull static List<Point> decode(@NonNull final String encodedPath, int precision); @NonNull static String encode(@NonNull final List<Point> path, int precision); @NonNull static List<Point> simplify(@NonNull List<Point> points); @NonNull static List<Point> simplify(@NonNull List<Point> points, double tolerance); @NonNull static List<Point> simplify(@NonNull List<Point> points, boolean highestQuality); @NonNull static List<Point> simplify(@NonNull List<Point> points, double tolerance,
                                     boolean highestQuality);  }
@Test public void testDecodePath() { List<Point> latLngs = decode(TEST_LINE, PRECISION_5); int expectedLength = 21; assertEquals("Wrong length.", expectedLength, latLngs.size()); Point lastPoint = latLngs.get(expectedLength - 1); expectNearNumber(37.76953, lastPoint.latitude(), 1e-6); expectNearNumber(-122.41488, lastPoint.longitude(), 1e-6); }
@Test public void testFromPolylineAndDecode() { List<Point> path1 = LineString.fromPolyline(TEST_LINE6, PRECISION_6).coordinates(); List<Point> path2 = decode(TEST_LINE6, PRECISION_6); assertEquals(path1.size(), path2.size()); for (int i = 0; i < path1.size(); i++) { assertEquals(path1.get(i), path2.get(i)); } }
@Test public void decode_neverReturnsNullButRatherAnEmptyList() throws Exception { List<Point> path = decode("", PRECISION_5); assertNotNull(path); assertEquals(0, path.size()); }