简体   繁体   English

计算两个地址php之间的距离

[英]Calculate distance between two address php

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. 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." 如果输入的用户地址距主要地址25英里或25英里以上,则弹出窗口将显示“对不起。您的地址不在我们的服务范围内,我们目前无法向您运送/提货设备。” If user's address is 5 miles away or less, no extra charge. 如果用户的地址在5英里以下,则无需额外付费。 If user's address is 5-10 miles away, $20 extra charge. 如果用户的地址在5-10英里之外,则需要加收20美元的费用。 If user's address is 10-25 mils away, $30 extra charge. 如果用户的地址在10到25密耳之间,则需要额外支付30美元。 Also any order that is $500 or more, no matter the distance (0-25 miles), no extra charge. 同样,任何订单金额为$ 500或更多,无论距离(0-25英里)如何,都无需额外付费。

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 使用Google Maps,PHP和MySQL从A点到B点的距离

How can make sure for the correct addresses? 如何确保输入正确的地址? Any alternate will be highly appreciated. 任何替代将不胜感激。 Thanks 谢谢

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 ). 对于前者,您应该能够在不使用外部API的情况下从PHP中计算两个纬度/经度对之间的距离,并且仅使用Google API从地址中检索坐标(请参阅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. 您可以验证Google API是否为用户输入的地址返回结果,并显示错误,要求他们确认输入的地址(如果没有输入)。

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. 或者,如果您在接受用户地址的页面中包含Google Maps控件,则可以增加获得Google API具有经/纬度数据的地址的可能性,并且还可以直接获取纬度和经度,而无需输入单独的API调用。

<?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;
?>
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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM