From 48b68fd2ba51210cb0793239598769efb56472d8 Mon Sep 17 00:00:00 2001 From: ichchha Date: Wed, 26 Feb 2020 12:09:51 +0545 Subject: [PATCH] pushed decode method to toaster library --- .../navigationlibrary/BaatoUtil.java | 67 +++++++++++++++++++ .../navigationlibrary/ToasterMessage.java | 1 - .../navigationlibrary/models/Geometry.java | 11 +++ 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 toasterlibrary/src/main/java/com/kathmandulivinglabs/navigationlibrary/BaatoUtil.java create mode 100644 toasterlibrary/src/main/java/com/kathmandulivinglabs/navigationlibrary/models/Geometry.java diff --git a/toasterlibrary/src/main/java/com/kathmandulivinglabs/navigationlibrary/BaatoUtil.java b/toasterlibrary/src/main/java/com/kathmandulivinglabs/navigationlibrary/BaatoUtil.java new file mode 100644 index 0000000..71fedc4 --- /dev/null +++ b/toasterlibrary/src/main/java/com/kathmandulivinglabs/navigationlibrary/BaatoUtil.java @@ -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> decodePolyline(String encoded, boolean is3D) { + List> 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 list = new ArrayList<>(); + list.add((double) lat / 1e5); + list.add((double) lng / 1e5); + list.add((double) ele / 100); + pointList.add(list); + } else { + List list = new ArrayList<>(); + list.add((double) lat / 1e5); + list.add((double) lng / 1e5); + pointList.add(list); + } + } + return pointList; + } + + +} diff --git a/toasterlibrary/src/main/java/com/kathmandulivinglabs/navigationlibrary/ToasterMessage.java b/toasterlibrary/src/main/java/com/kathmandulivinglabs/navigationlibrary/ToasterMessage.java index 29c04ff..945b554 100644 --- a/toasterlibrary/src/main/java/com/kathmandulivinglabs/navigationlibrary/ToasterMessage.java +++ b/toasterlibrary/src/main/java/com/kathmandulivinglabs/navigationlibrary/ToasterMessage.java @@ -8,6 +8,5 @@ public class ToasterMessage { public static void s(Context c, String message){ Toast.makeText(c,message,Toast.LENGTH_SHORT).show(); - } } \ No newline at end of file diff --git a/toasterlibrary/src/main/java/com/kathmandulivinglabs/navigationlibrary/models/Geometry.java b/toasterlibrary/src/main/java/com/kathmandulivinglabs/navigationlibrary/models/Geometry.java new file mode 100644 index 0000000..a36e11e --- /dev/null +++ b/toasterlibrary/src/main/java/com/kathmandulivinglabs/navigationlibrary/models/Geometry.java @@ -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; + } +}