Jump to content

Distance calculating function problem


ukscotth

Recommended Posts

Hi,

 

I have been using the following function on my server and it works fine but when I try and use it on a different server it wont seem to work. Any ideas on why it wont work on the new server and any ideas on how I can do some error checking to figure out why ?

 

Thanks in advance,

 

Scott

 

<?php
function get_driving_information($start, $finish, $raw = false)
{
     # Convert any lon / lat coordingates
     if(preg_match('@-?[0-9]+\.?[0-9]*\s*,\s*-?[0-9]+\.?[0-9]@', $start))
     {
         $url = 'http://maps.google.co.uk/m/local?q='.urlencode($start);
         if($data = file_get_contents($url))
         {
             if(preg_match('@<div class="cpfxql"><a href="[^"]*defaultloc=(.*?)&ll=@smi', $data, $found))
             {
                 $start = trim(urldecode($found[1]));
             }
             else
             {
                 throw new Exception('Start lon / lat coord conversion failed');
             }
         }
         else
         {
             throw new Exception('Start lon / lat coord conversion failed');
         }
     }

    if(preg_match('@-?[0-9]+\.?[0-9]*\s*,\s*-?[0-9]+\.?[0-9]@', $finish))
     {
         $url = 'http://maps.google.co.uk/m/local?q='.urlencode($finish);
         if($data = file_get_contents($url))
         {
             if(preg_match('@<div class="cpfxql"><a href="[^"]*defaultloc=(.*?)&ll=@smi', $data, $found))
             {
                 $finish = trim(urldecode($found[1]));
             }
             else
             {
                 throw new Exception('Finish lon / lat coord conversion failed');
             }
         }
         else
         {
             throw new Exception('Finish lon / lat coord conversion failed');
         }
     }

    if(strcmp($start, $finish) == 0)
     {
         $time = 0;
         if($raw)
         {
             $time .= ' seconds';
         }

        return array('distance' => 0, 'time' => $time);
     }

    $start  = urlencode($start);
     $finish = urlencode($finish);

    $distance   = 'unknown';
     $time       = 'unknown';

    $url = 'http://maps.google.co.uk/m/directions?saddr='.$start.'&daddr='.$finish.'&hl=en&oi=nojs&dirflg=d';
     if($data = file_get_contents($url))
     {
         if(preg_match('@<span[^>]+>([^<]+) (mi|km)</span>@smi', $data, $found))
         {
             $distanceNum    = trim($found[1]);
             $distanceUnit   = trim($found[2]);

            if($raw)
             {
                 $distance = $distanceNum.' '.$distanceUnit;
             }
             else
             {
                 $distance = number_format($distanceNum, 2);
                 if(strcmp($distanceUnit, 'km') == 0)
                 {
                     $distance = $distanceNum / 1.609344;
                 }
             }
         }
         else
         {
             throw new Exception('Could not find that route');
         }

        if(preg_match('@<b>([^<]*)</b>@smi', $data, $found))
         {
             $timeRaw = trim($found[1]);

            if($raw)
             {
                 $time = $timeRaw;
             }
             else
             {
                 $time = 0;

                $parts = preg_split('@days?@i', $timeRaw);
                 if(count($parts) > 1)
                 {
                     $time += (86400 * $parts[0]);
                     $timeRaw = $parts[1];
                 }

                $parts = preg_split('@hours?@i', $timeRaw);
                 if(count($parts) > 1)
                 {
                     $time += (3600 * $parts[0]);
                     $timeRaw = $parts[1];
                 }

                $time += (60 * (int)$timeRaw);
             }
         }

        return array('distance' => $distance, 'time' => $time);
     }
     else
     {
         throw new Exception('Could not resolve URL');
     }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.