@Test public void testDecodePath() { List<LatLng> latLngs = PolyUtil.decode(TEST_LINE); int expectedLength = 21; assertEquals("Wrong length.", expectedLength, latLngs.size()); LatLng lastPoint = latLngs.get(expectedLength - 1); assertEquals(37.76953, lastPoint.latitude, 1e-6); assertEquals(-122.41488, lastPoint.longitude, 1e-6); }
public static List<LatLng> decode(final String encodedPath) { int len = encodedPath.length(); final List<LatLng> path = new ArrayList<LatLng>(); int index = 0; int lat = 0; int lng = 0; while (index < len) { int result = 1; int shift = 0; int b; do { b = encodedPath.charAt(index++) - 63 - 1; result += b << shift; shift += 5; } while (b >= 0x1f); lat += (result & 1) != 0 ? ~(result >> 1) : (result >> 1); result = 1; shift = 0; do { b = encodedPath.charAt(index++) - 63 - 1; result += b << shift; shift += 5; } while (b >= 0x1f); lng += (result & 1) != 0 ? ~(result >> 1) : (result >> 1); path.add(new LatLng(lat * 1e-5, lng * 1e-5)); } return path; }
PolyUtil { public static List<LatLng> decode(final String encodedPath) { int len = encodedPath.length(); final List<LatLng> path = new ArrayList<LatLng>(); int index = 0; int lat = 0; int lng = 0; while (index < len) { int result = 1; int shift = 0; int b; do { b = encodedPath.charAt(index++) - 63 - 1; result += b << shift; shift += 5; } while (b >= 0x1f); lat += (result & 1) != 0 ? ~(result >> 1) : (result >> 1); result = 1; shift = 0; do { b = encodedPath.charAt(index++) - 63 - 1; result += b << shift; shift += 5; } while (b >= 0x1f); lng += (result & 1) != 0 ? ~(result >> 1) : (result >> 1); path.add(new LatLng(lat * 1e-5, lng * 1e-5)); } return path; } }
PolyUtil { public static List<LatLng> decode(final String encodedPath) { int len = encodedPath.length(); final List<LatLng> path = new ArrayList<LatLng>(); int index = 0; int lat = 0; int lng = 0; while (index < len) { int result = 1; int shift = 0; int b; do { b = encodedPath.charAt(index++) - 63 - 1; result += b << shift; shift += 5; } while (b >= 0x1f); lat += (result & 1) != 0 ? ~(result >> 1) : (result >> 1); result = 1; shift = 0; do { b = encodedPath.charAt(index++) - 63 - 1; result += b << shift; shift += 5; } while (b >= 0x1f); lng += (result & 1) != 0 ? ~(result >> 1) : (result >> 1); path.add(new LatLng(lat * 1e-5, lng * 1e-5)); } return path; } private  PolyUtil(); }
PolyUtil { public static List<LatLng> decode(final String encodedPath) { int len = encodedPath.length(); final List<LatLng> path = new ArrayList<LatLng>(); int index = 0; int lat = 0; int lng = 0; while (index < len) { int result = 1; int shift = 0; int b; do { b = encodedPath.charAt(index++) - 63 - 1; result += b << shift; shift += 5; } while (b >= 0x1f); lat += (result & 1) != 0 ? ~(result >> 1) : (result >> 1); result = 1; shift = 0; do { b = encodedPath.charAt(index++) - 63 - 1; result += b << shift; shift += 5; } while (b >= 0x1f); lng += (result & 1) != 0 ? ~(result >> 1) : (result >> 1); path.add(new LatLng(lat * 1e-5, lng * 1e-5)); } return path; } 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 List<LatLng> decode(final String encodedPath) { int len = encodedPath.length(); final List<LatLng> path = new ArrayList<LatLng>(); int index = 0; int lat = 0; int lng = 0; while (index < len) { int result = 1; int shift = 0; int b; do { b = encodedPath.charAt(index++) - 63 - 1; result += b << shift; shift += 5; } while (b >= 0x1f); lat += (result & 1) != 0 ? ~(result >> 1) : (result >> 1); result = 1; shift = 0; do { b = encodedPath.charAt(index++) - 63 - 1; result += b << shift; shift += 5; } while (b >= 0x1f); lng += (result & 1) != 0 ? ~(result >> 1) : (result >> 1); path.add(new LatLng(lat * 1e-5, lng * 1e-5)); } return path; } 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; }