Created
November 13, 2019 16:53
-
-
Save preetjdp/caa6d7a3b5f22d026dd0503e1a219071 to your computer and use it in GitHub Desktop.
This is the code required to decode the (encoded)polyline into a List of LatLng
This file contains hidden or 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
| import 'package:google_maps_flutter/google_maps_flutter.dart'; | |
| List<LatLng> decodePolyline(final String encodedPath) { | |
| int len = encodedPath.length; | |
| // For speed we preallocate to an upper bound on the final length, then | |
| // truncate the array before returning. | |
| final List<LatLng> path = []; | |
| int index = 0; | |
| int lat = 0; | |
| int lng = 0; | |
| while (index < len) { | |
| int result = 1; | |
| int shift = 0; | |
| int b; | |
| do { | |
| b = encodedPath.codeUnitAt(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.codeUnitAt(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; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment