@extends('layouts.app') @section('center')

Devices Distance

Export to Excel
@php function haversineDistance($lat1, $lon1, $lat2, $lon2, $earthRadius = 6371) { $latFrom = deg2rad($lat1); $lonFrom = deg2rad($lon1); $latTo = deg2rad($lat2); $lonTo = deg2rad($lon2); $latDelta = $latTo - $latFrom; $lonDelta = $lonTo - $lonFrom; $angle = 2 * asin(sqrt(pow(sin($latDelta / 2), 2) + cos($latFrom) * cos($latTo) * pow(sin($lonDelta / 2), 2))); return $earthRadius * $angle; } $previousDevice = null; $totalDistance = 0; $firstpooldistance = null; $i = 1; @endphp {{--transformers--}} @foreach($devices as $device) @php $previousDevice = null; @endphp {{--Pools--}} @foreach($device->children as $child) @php $previousDevice = $child; @endphp @endforeach @endforeach
# Global Device ID Type Latitude Longitude Distance (km)
{{ $i++ }} {{ $device->global_device_id }} {{ $device->installed_on }} {{ $device->latitude }} {{ $device->longitude }} N/A
{{ $i++ }}    ↳ {{ $child->global_device_id }} {{ $child->installed_on }} {{ $child->latitude }} {{ $child->longitude }} @if($previousDevice) @php $distance = haversineDistance( $previousDevice->latitude, $previousDevice->longitude, $child->latitude, $child->longitude ); $totalDistance += $distance; @endphp @if($distance < 1) {{ number_format($distance * 1000, 0) }} m @else {{ number_format($distance, 2) }} km @endif @else @php $firstpooldistance = haversineDistance( $device->latitude, $device->longitude, $child->latitude, $child->longitude ); $totalDistance += $firstpooldistance; @endphp @if($firstpooldistance < 1) {{ number_format($firstpooldistance * 1000, 0) }} m @else {{ number_format($firstpooldistance, 2) }} km @endif @endif
Total Distance ({{ $device->global_device_id }}) {{ number_format($totalDistance, 2) }} km
@push('scripts') @endpush @endsection()