encodePolyline static method
Encodes a list of LatLng into a polyline string.
This method takes a list of LatLng coordinates and converts them into a Google Maps polyline string representation. The polyline string can be used in Google Maps to render the path defined by the coordinates.
Implementation
static String encodePolyline(List<LatLng> coordinates) {
StringBuffer encoded = StringBuffer();
int lastLat = 0; // Holds the last latitude value
int lastLng = 0; // Holds the last longitude value
// Iterate through each point in the list of coordinates
for (var point in coordinates) {
int lat = (point.latitude * 1E5).round(); // Convert latitude to integer
int lng = (point.longitude * 1E5).round(); // Convert longitude to integer
// Calculate the difference from the last point
int dLat = lat - lastLat;
int dLng = lng - lastLng;
// Encode the difference and append to the result
encoded.write(_encodeValue(dLat));
encoded.write(_encodeValue(dLng));
// Update the last latitude and longitude for the next iteration
lastLat = lat;
lastLng = lng;
}
return encoded.toString(); // Return the encoded polyline string
}