Skip to content

Commit 48b68fd

Browse files
committed
pushed decode method to toaster library
1 parent 6e3bd5f commit 48b68fd

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.kathmandulivinglabs.navigationlibrary;
2+
3+
import com.kathmandulivinglabs.navigationlibrary.models.Geometry;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class BaatoUtil {
9+
public static Geometry getGeoJsonFromEncodedPolyLine(String encoded) {
10+
return new Geometry("LineString", decodePolyline(encoded, false));
11+
}
12+
13+
public static List<List<Double>> decodePolyline(String encoded, boolean is3D) {
14+
List<List<Double>> pointList = new ArrayList<>();
15+
int index = 0;
16+
int len = encoded.length();
17+
int lat = 0, lng = 0, ele = 0;
18+
while (index < len) {
19+
// latitude
20+
int b, shift = 0, result = 0;
21+
do {
22+
b = encoded.charAt(index++) - 63;
23+
result |= (b & 0x1f) << shift;
24+
shift += 5;
25+
} while (b >= 0x20);
26+
int deltaLatitude = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
27+
lat += deltaLatitude;
28+
29+
// longitute
30+
shift = 0;
31+
result = 0;
32+
do {
33+
b = encoded.charAt(index++) - 63;
34+
result |= (b & 0x1f) << shift;
35+
shift += 5;
36+
} while (b >= 0x20);
37+
int deltaLongitude = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
38+
lng += deltaLongitude;
39+
40+
if (is3D) {
41+
// elevation
42+
shift = 0;
43+
result = 0;
44+
do {
45+
b = encoded.charAt(index++) - 63;
46+
result |= (b & 0x1f) << shift;
47+
shift += 5;
48+
} while (b >= 0x20);
49+
int deltaElevation = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
50+
ele += deltaElevation;
51+
List<Double> list = new ArrayList<>();
52+
list.add((double) lat / 1e5);
53+
list.add((double) lng / 1e5);
54+
list.add((double) ele / 100);
55+
pointList.add(list);
56+
} else {
57+
List<Double> list = new ArrayList<>();
58+
list.add((double) lat / 1e5);
59+
list.add((double) lng / 1e5);
60+
pointList.add(list);
61+
}
62+
}
63+
return pointList;
64+
}
65+
66+
67+
}

toasterlibrary/src/main/java/com/kathmandulivinglabs/navigationlibrary/ToasterMessage.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,5 @@ public class ToasterMessage {
88
public static void s(Context c, String message){
99

1010
Toast.makeText(c,message,Toast.LENGTH_SHORT).show();
11-
1211
}
1312
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.kathmandulivinglabs.navigationlibrary.models;
2+
3+
public class Geometry {
4+
public String type;
5+
public Object coordinates;
6+
7+
public Geometry(String type, Object coordinates) {
8+
this.type = type;
9+
this.coordinates = coordinates;
10+
}
11+
}

0 commit comments

Comments
 (0)