54 lines
1.4 KiB
Dart
54 lines
1.4 KiB
Dart
import 'package:latlong2/latlong.dart';
|
|
|
|
List<LatLng> safeDecodePolyline(String encoded) {
|
|
if (encoded.isEmpty) return [];
|
|
try {
|
|
List<LatLng> poly = [];
|
|
int index = 0, len = encoded.length;
|
|
int lat = 0, lng = 0;
|
|
|
|
while (index < len) {
|
|
int b, shift = 0, result = 0;
|
|
do {
|
|
if (index >= len) break;
|
|
b = encoded.codeUnitAt(index++) - 63;
|
|
result |= (b & 0x1f) << shift;
|
|
shift += 5;
|
|
} while (b >= 0x20);
|
|
|
|
// Dart Web bitwise operations (~ and >>) can cause 32-bit unsigned wrap-around
|
|
// Using arithmetic avoids the issue where lat becomes 42995.xxxx (offset by 2^32)
|
|
int dlat = (result & 1) != 0 ? -((result >> 1) + 1) : (result >> 1);
|
|
lat += dlat;
|
|
// Correction manuelle au cas où un wrap unsigned 32-bit s'est produit
|
|
if (lat > 2147483647) lat -= 4294967296;
|
|
|
|
shift = 0;
|
|
result = 0;
|
|
do {
|
|
if (index >= len) break;
|
|
b = encoded.codeUnitAt(index++) - 63;
|
|
result |= (b & 0x1f) << shift;
|
|
shift += 5;
|
|
} while (b >= 0x20);
|
|
|
|
int dlng = (result & 1) != 0 ? -((result >> 1) + 1) : (result >> 1);
|
|
lng += dlng;
|
|
if (lng > 2147483647) lng -= 4294967296;
|
|
|
|
double finalLat = lat / 1e5;
|
|
double finalLng = lng / 1e5;
|
|
|
|
poly.add(LatLng(finalLat, finalLng));
|
|
}
|
|
|
|
return poly;
|
|
} catch (e) {
|
|
// ignore: avoid_print
|
|
print('[POLYLINE] Erreur décodage: $e');
|
|
return [];
|
|
}
|
|
}
|
|
|
|
|