I\'m trying to use the Google directions API to show directions on my mapview but I am having difficulties getting the data from the JSON response. I can get the \"levels\"
GeoPoint doesn't work for me, I cant find the library that uses it. Here is a function that returns LatLng values instead.
public static ArrayList decodePolyPoints(String encodedPath){
int len = encodedPath.length();
final ArrayList path = new ArrayList();
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;
}
Grabbed it from Google Maps Android API utility library. Code can be found here
Some reminders when testing this with hard coded strings in the code, Java cannot correctly read
"\"
you need to add another slash for it to be correctly read by java.
"\\"
Just a heads up because encoded strings contain weird characters.