Jump to content

[SOLVED] Calculate Driving Distance


millercj

Recommended Posts

I'm building a website for my client with an e-commerce component. She wants to offer free delivery if the address of the buyer is within 30 driving miles of her home. I know if I go to the URL:

 

http://maps.google.com/maps?q=from+A+to+B&output=kml

 

with A being the starting address and B being the ending address it will return the data in which that information is encoded. Is there a way to do something like this within the backend so the consumer never sees anything?

Link to comment
https://forums.phpfreaks.com/topic/145570-solved-calculate-driving-distance/
Share on other sites

i figured out that I can do it with ajax without api's:

 

$prepa = "XXXX";
$prepb = "XXXX";
$a=urlencode($prepa);
$b=urlencode($prepb);
//Create the url
$myurl = "http://maps.google.com/maps?q=from+{$a}+to+{$b}&output=kml";
//Open the url
$f = fopen ($myurl, "r");
//Read the contents
$str = stream_get_contents($f);
//String match the distance
preg_match("/Distance: ([0-9.-]+)/", $str, $distance);
preg_match("/#160;([a-z]+)/", $str, $units);
//Respond

if($distance[1]!=NULL && $distance[1]<30)
{echo "Eligible for Delivery".$distance[1].$units[1];}
else{echo "Not Eligible for Delivery ".$distance[1].$units[1];}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.