Fix for polyline encoding

This commit is contained in:
Ben Goldsworthy 2023-01-10 20:00:48 +00:00
parent 87a0d95435
commit c55d572ec4
2 changed files with 52 additions and 2 deletions

View file

@ -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;
}
}

View file

@ -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
], {