Skip to content

Commit

Permalink
pushed decode method to toaster library
Browse files Browse the repository at this point in the history
  • Loading branch information
Ichchhie committed Feb 26, 2020
1 parent 6e3bd5f commit 48b68fd
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.kathmandulivinglabs.navigationlibrary;

import com.kathmandulivinglabs.navigationlibrary.models.Geometry;

import java.util.ArrayList;
import java.util.List;

public class BaatoUtil {
public static Geometry getGeoJsonFromEncodedPolyLine(String encoded) {
return new Geometry("LineString", decodePolyline(encoded, false));
}

public static List<List<Double>> decodePolyline(String encoded, boolean is3D) {
List<List<Double>> pointList = new ArrayList<>();
int index = 0;
int len = encoded.length();
int lat = 0, lng = 0, ele = 0;
while (index < len) {
// latitude
int b, shift = 0, result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int deltaLatitude = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lat += deltaLatitude;

// longitute
shift = 0;
result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int deltaLongitude = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lng += deltaLongitude;

if (is3D) {
// elevation
shift = 0;
result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int deltaElevation = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
ele += deltaElevation;
List<Double> list = new ArrayList<>();
list.add((double) lat / 1e5);
list.add((double) lng / 1e5);
list.add((double) ele / 100);
pointList.add(list);
} else {
List<Double> list = new ArrayList<>();
list.add((double) lat / 1e5);
list.add((double) lng / 1e5);
pointList.add(list);
}
}
return pointList;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ public class ToasterMessage {
public static void s(Context c, String message){

Toast.makeText(c,message,Toast.LENGTH_SHORT).show();

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.kathmandulivinglabs.navigationlibrary.models;

public class Geometry {
public String type;
public Object coordinates;

public Geometry(String type, Object coordinates) {
this.type = type;
this.coordinates = coordinates;
}
}

0 comments on commit 48b68fd

Please sign in to comment.