0

I've a situation where clients ask me to calculate the distance between main address and customer address. And based on the distance I've to add some additional charge to my cart and some validation and showing customer that his address is out of service.

Below is description;

Main address; 2801 Florida Ave, Miami Fl, 33133.

If user's address entered is 25 miles or more from Main Address, a pop up will say "We are sorry. Your address falls outside of our service area and we are unable to deliver/pick up equipment to you at this time." If user's address is 5 miles away or less, no extra charge. If user's address is 5-10 miles away, $20 extra charge. If user's address is 10-25 mils away, $30 extra charge. Also any order that is $500 or more, no matter the distance (0-25 miles), no extra charge.

I've came up with below answer and thinking, is it the solution for my problem?

Distance from point A to B using Google Maps, PHP and MySQL

How can make sure for the correct addresses? Any alternate will be highly appreciated. Thanks

Community
  • 1
  • 1
Irfan
  • 4,882
  • 12
  • 52
  • 62

3 Answers3

1

The requirement doesn't specify whether the distance should be a straight line from the base address to the user's address, or if instead it should take into account actual distance on roads.

For the former, you should be able to calculate the distance between two lat/long pairs from within PHP without using an external API, and only use the Google API to retrieve the coordinates from the addresses (take a look at the Google Geocoding API). For the latter, the question/answer you posted seems appropriate.

Ensuring that the addresses are correct is just a matter of validating the input. You can verify that Google API returns a result for the address the user enters and display an error asking them to confirm the address they entered if it doesn't.

Alternately, if you include a Google Maps control in the page where you accept the user's address you can increase the likelihood that you're getting an address that the Google API has lat/long data for and also directly get the latitude and longitude without a separate API call.

Sam
  • 4,994
  • 4
  • 30
  • 37
  • 1
    To expand on this: If you wanted to compute the distance between two sets of coordinates, you would use the [haversine formula](http://stackoverflow.com/a/14751773/3250005). Note that the distance given by the formula will be "as the bird flies" so it will be a bit less than driving distance. – Mathew Tinsley Jun 14 '15 at 04:59
  • And if you care about performance with doing these types of queries check out the [MySQL geospatial extensions](https://dev.mysql.com/doc/refman/5.5/en/spatial-extensions.html). – doublesharp Jun 16 '15 at 18:06
1
<?php 
$from = "kathmandu,nepal";
$to = "mahendranagar,nepal";
$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/>";
$distance=$distance/1000;
echo "Distance: ".$distance." km";
$default_price=11;
$distance_mile=$distance*1.6;
$price_distance=$default_price*$distance_mile;

if($distance_mile>=25){
    echo "<script>alert('We are sorry. Your address falls outside of our service area and we are unable to deliver/pick up equipment to you at this time.');</script>";
}
elseif($price_distance>=500){
    $price=$price_distance;
}else{
    if($distance_mile<5){
        $price=$price_distance;
    }elseif($distance_mile>5 && $distance_mile<10){
        $price=$default_price + 20;
    }
    elseif($distance_mile>10 && $distance_mile<25){
        $price=$default_price + 30;
    }
}
echo "final price : ".$price;
?>
-1
function getDistance($addressFrom, $addressTo, $unit){
//Change address format
$formattedAddrFrom = str_replace(' ','+',$addressFrom);
$formattedAddrTo   = str_replace(' ','+',$addressTo);

//Send request and receive json data
$geocodeFrom = file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$formattedAddrFrom.'&sensor=false');
$outputFrom  = json_decode($geocodeFrom);
$geocodeTo   = file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$formattedAddrTo.'&sensor=false');
$outputTo    = json_decode($geocodeTo);

//Get latitude and longitude from geo data
$latitudeFrom  = $outputFrom->results[0]->geometry->location->lat;
$longitudeFrom = $outputFrom->results[0]->geometry->location->lng;
$latitudeTo    = $outputTo->results[0]->geometry->location->lat;
$longitudeTo   = $outputTo->results[0]->geometry->location->lng;

//Calculate distance from latitude and longitude
$theta = $longitudeFrom - $longitudeTo;
$dist  = sin(deg2rad($latitudeFrom)) * sin(deg2rad($latitudeTo)) +  cos(deg2rad($latitudeFrom)) * cos(deg2rad($latitudeTo)) * cos(deg2rad($theta));
$dist  = acos($dist);
$dist  = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit  = strtoupper($unit);
if ($unit == "K") {
    return ($miles * 1.609344).' km';
} else if ($unit == "N") {
    return ($miles * 0.8684).' nm';
} else {
    return $miles.' mi';
}}

// You can use this function like the below.
$addressFrom = 'Insert from address';
$addressTo = 'Insert to address';
$distance = getDistance($addressFrom, $addressTo, "K");
echo $distance;
kamleshpal
  • 13
  • 4