0

I have been desperately trying to make a flight distance calculator using Google API.

The closest I have come is getting road distance, using an answer posted on another SO question (Distance from point A to B using Google Maps, PHP and MySQL), which was as follows:

$from = "Więckowskiego 72, Łódź";
$to = "Gazowa 1, Łódź";

$from = urlencode($from);
$to = urlencode($to);

$data = file_get_contents("http://maps.googleapis.com/maps/api/distancematrix/json?origins=$from&destinations=$to&language=en-EN&sensor=false");
$data = json_decode($data);

$time = 0;
$distance = 0;

foreach($data->rows[0]->elements as $road) {
    $time += $road->duration->value;
    $distance += $road->distance->value;
}

echo "To: ".$data->destination_addresses[0];
echo "<br/>";
echo "From: ".$data->origin_addresses[0];
echo "<br/>";
echo "Time: ".$time." seconds";
echo "<br/>";
echo "Distance: ".$distance." meters";

Output:

To: Gazowa 1, 91-076 Łódź, Poland
From: Więckowskiego 72, Łódź, Poland
Time: 206 seconds
Distance: 1488 meters

Is there any simple way of getting the above to provide the distance as the crow flies?

Simon H
  • 2,495
  • 4
  • 30
  • 38
  • Recently someone asked this question, is that what you are looking for? To find the distance between two points? http://stackoverflow.com/a/41353948/534862 – Lionel Chan Dec 31 '16 at 13:52
  • According to Google, the distancematrix API supports **travel_mode** as a parameter - however your options are `driving`, `walking`, `bicycling` or `transit` so the API is useless. I suggest you checkout their Geometry library https://developers.google.com/maps/documentation/javascript/geometry – Daniel Dec 31 '16 at 14:02

0 Answers0