From c55d572ec4ff039a9e678b732536a89e25afcd7f Mon Sep 17 00:00:00 2001 From: Rumperuu Date: Tue, 10 Jan 2023 20:00:48 +0000 Subject: [PATCH] Fix for polyline encoding --- app/Http/Controllers/TrackerController.php | 52 +++++++++++++++++++++- resources/views/tracker.blade.php | 2 +- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/TrackerController.php b/app/Http/Controllers/TrackerController.php index 57bd9da..a0dd212 100644 --- a/app/Http/Controllers/TrackerController.php +++ b/app/Http/Controllers/TrackerController.php @@ -141,7 +141,7 @@ class TrackerController extends Controller $client = new Client([ 'base_uri' => 'https://app.wayward.travel/', - 'timeout' => 10.0 + 'timeout' => 20.0 ]); $response = $client->get('trip/'.($tripId ?? config('app.current_trip_id')).'/user/zmld8ko6qy7d9j3xvq10/json'); @@ -173,10 +173,60 @@ class TrackerController extends Controller } } + $data->trip->locations = $this->decode_polyline($data->trip->route); + return $data->trip; default: // TODO: Add proper error handling. return "Something went wrong"; } } + + /* + * Decode route polyline. + * + * Source: https://github.com/dyaaj/polyline-encoder + * + * @param object $value + * @return object + */ + public function decode_polyline($value) + { + $index = 0; + $points = array(); + $lat = 0; + $lng = 0; + $id = 0; + + while ($index < strlen($value)) { + $b; + $shift = 0; + $result = 0; + do { + $b = ord(substr($value, $index++, 1)) - 63; + $result |= ($b & 0x1f) << $shift; + $shift += 5; + } while ($b > 31); + $dlat = (($result & 1) ? ~($result >> 1) : ($result >> 1)); + $lat += $dlat; + + $shift = 0; + $result = 0; + do { + $b = ord(substr($value, $index++, 1)) - 63; + $result |= ($b & 0x1f) << $shift; + $shift += 5; + } while ($b > 31); + $dlng = (($result & 1) ? ~($result >> 1) : ($result >> 1)); + $lng += $dlng; + + array_push($points, (object)[ + 'id' => $id++, + 'latitude' => $lat/100000, + 'longitude'=> $lng/100000 + ]); + } + + return $points; + } } diff --git a/resources/views/tracker.blade.php b/resources/views/tracker.blade.php index 7bfdf6b..fb60e1d 100644 --- a/resources/views/tracker.blade.php +++ b/resources/views/tracker.blade.php @@ -63,7 +63,7 @@ var routePoints = L.polyline([ @foreach($trip->locations as $location) @if(!in_array($location->id, config('app.current_trip_ignore'))) - [{{ $location->latitude }}, {{ $location->longitude }}], + [{{ $location->latitude }}, {{ $location->longitude }}], // {{$location->id}} @endif @endforeach ], {