vendredi 30 octobre 2020

How can we pass a variable from script inside php and set it to new a variable?(Updated)

How can we pass a variable from script inside php and set it to new a variable? I want to calculate the user location and the destination location by their coordinates but first I want to get the user coordinates which is already given in the script.

<?php
function distance($lat1, $lon1, $lat2, $lon2, $unit) {
  if (($lat1 == $lat2) && ($lon1 == $lon2)) {
    return 0;
  }
  else {
    $theta = $lon1 - $lon2;
    $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
    $dist = acos($dist);
    $dist = rad2deg($dist);
    $miles = $dist * 60 * 1.1515;
    $unit = strtoupper($unit);

    if ($unit == "K") {
      return ($miles * 1.609344);
    } else if ($unit == "N") {
      return ($miles * 0.8684);
    } else {
      return $miles;
    }
  }
}

$mResult = distance($lat, $long, 29.46786, -98.53506, "M") . " Miles<br>";
$kmResult = distance($lat, $long, 29.46786, -98.53506, "K") . " Kilometers<br>";
$nmResult = distance($lat, $long, 29.46786, -98.53506, "N") . " Nautical Miles<br>";

echo $kmResult;

?>
<script>

window.onload = function getLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(bindPosition);
  } else { 
    y.innerHTML = "Geolocation is not supported by this browser.";
  }
}

function bindPosition(position) {
  $.ajax({
    url: "/getlocation",
    type: "GET",
    dataType: "json",
    data: { 
      lat: position.coords.latitude, 
      long: position.coords.longitude
    },
    success: function(response){
          if(data == "success")
        alert(response); 
    },
    error: function(response){
        alert('Error'+response);
        console.log('Error'+response);
    }
  })
}
</script>

the route

Route::get('getlocation', 'HomeController@getAllLocations');

ajax response enter image description here

enter image description here



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire