Last active
April 25, 2020 06:09
-
-
Save debkanchan/b428e052279b7124b8edb974536f47f9 to your computer and use it in GitHub Desktop.
Flutter/Dart Google Maps API encoded polyline decoder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List _decode(String input) { | |
var list=input.codeUnits; | |
List lList = new List(); | |
int index=0; | |
int len=input.length; | |
int c=0; | |
List<LatLng> positions = new List(); | |
// repeating until all attributes are decoded | |
do { | |
var shift=0; | |
int result=0; | |
// for decoding value of one attribute | |
do { | |
c=list[index]-63; | |
result|=(c & 0x1F)<<(shift*5); | |
index++; | |
shift++; | |
} while(c>=32); | |
/* if value is negetive then bitwise not the value */ | |
if(result & 1==1) { | |
result=~result; | |
} | |
var result1 = (result >> 1) * 0.00001; | |
lList.add(result1); | |
} while(index<len); | |
/*adding to previous value as done in encoding */ | |
for(int i=2;i<lList.length;i++) { | |
lList[i]+=lList[i-2]; | |
} | |
for(int i=0; i<lList.length; i+=2) { | |
positions.add(LatLng(lList[i], lList[i+1])); | |
} | |
return positions; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment