mgsmus gpt nin kodu abi bu ya ben daha yazmadım örnek olsun diye sordum
Son hali bu şekilde abi aslında ama çok geç cevap dönüyor birden fazla makine ile metre cinsinden kıyas yapmam ve metreye göre geri dönüş yapmam gerekiyor ama pazar olunca fazla üstüne düşmedim 😄
public function machhineFleet(Request $request){
$url = 'https://api.distancematrix.ai/maps/api/distancematrix/json';
$apiKey=ds';
$userLat = $request->input('lat');
$userLng = $request->input('long');
$maxDistance = $request->input('meters');
$machineLocations = Machine::with(['commentsMachine', 'weeksday', 'fleets'])->get();
$allRoutes = [];
foreach ($machineLocations as $machineLocation) {
$params = [
'origins' => "$userLat,$userLng",
'destinations' => "{$machineLocation->lat},{$machineLocation->lng}",
'key' => $apiKey,
];
$averageRate = $machineLocation->commentsMachine->avg('rate');
$apiUrl = $url . '?' . http_build_query($params);
$response = json_decode(file_get_contents($apiUrl));
if ($response->status === "OK") {
$routes = [];
foreach ($response->rows[0]->elements as $element) {
if ($element->status !== "OK") {
continue;
}
$distance = $element->distance->value;
if ($distance <= $maxDistance) {
$distanceText = $element->distance->text;
$duration = $element->duration->text;
$routes[] = [
'distanceText' => $distanceText,
'duration' => $duration,
'id' => $machineLocation->id,
'lat'=>$machineLocation->lat,
'lng'=>$machineLocation->lng,
'name' => $machineLocation->name,
'average_rate' => $averageRate,
'distance' => $machineLocation->distance,
'weeksday' => $machineLocation->weeksday,
];
}
}
$allRoutes = array_merge($allRoutes, $routes);
} elseif ($response->status === "ZERO_RESULTS") {
continue;
} else {
return response()->json([
'error' => 'An error occurred while processing the request.',
], 500);
}
}
// Tüm rotaları döndür
return response()->json([
'routes' => $allRoutes,
], 200);
}