我必须找到提货和营业地址之间的距离,以及送货和营业地址之间的距离。
我通过调用geocode api来获取php代码,以从数据库获取具有动态地址输入的地址坐标。然后,它计算所需的距离(解决了问题的目的),但是由于要遍历所有这些事务,因此注册了数千个业务,因此会导致多个api调用。
我也尝试使用curl代替file_get_contents,但是没有任何区别。 我们可以减少api调用并使它更快吗。
/* this whole code is in loop for all businesses*/
/*address from database*/
$raddress=$rstreetno." ".$rstreetname.",".$rsuburb.",".$address_state.",".$rpostcode;
$mapsApiKey= get_option('shipme_radius_maps_api_key');
/* API Call*/
$urllocal = "https://maps.googleapis.com/maps/api/geocode/json?address=".urlencode($raddress)."&sensor=false&key=".$mapsApiKey;
$result_string = file_get_contents($urllocal);
$result = json_decode($result_string, true);
if(!empty($result['results'])){
$remoaddLat = $result['results'][0]['geometry']['location']['lat'];
$remoaddlng = $result['results'][0]['geometry']['location']['lng'];
}
$earthRadius = 6378;//earth radius in kms.
$latpickFrom = deg2rad($jobpickup_lat);
$lonpickFrom = deg2rad($jobpickup_lng);
$lataddress=deg2rad($remoaddLat);
$lngaddress=deg2rad($remoaddlng);
$latdelTo = deg2rad($jobdelivery_lat);
$londelTo = deg2rad($jobdelivery_lng);
$latDeltapickup = $lataddress- $latpickFrom;
$lonDeltapickup = $lngaddress - $lonpickFrom;
$latDeltadel = $latdelTo - $lataddress;
$lonDeltadel = $londelTo - $lngaddress;
$latjobdist = $latdelTo - $latpickFrom;
$lonjobdist = $londelTo - $lonpickFrom;
/*Distance between business address and pickup*/
$a = sin($latDeltapickup / 2) * sin($latDeltapickup / 2) +
cos($lataddress) * cos($latpickFrom) *
sin($lonDeltapickup / 2) * sin($lonDeltapickup / 2);
$c = 2 * asin(sqrt($a));
$distancefrompickup = round($earthRadius * $c,2);
/*Distance between business address and delivery*/
$a1 = sin($latDeltadel / 2) * sin($latDeltadel / 2) +
cos($lataddress) * cos($latdelTo) *
sin($lonDeltadel / 2) * sin($lonDeltadel / 2);
$c1 = 2 * asin(sqrt($a1));
$distancefromdel = round($earthRadius * $c1,2);
/*Distance between pickup and delivery*/
$a2 = sin($latjobdist / 2) * sin($latjobdist / 2) +
cos($latpickFrom) * cos($latdelTo) *
sin($lonjobdist / 2) * sin($lonjobdist / 2);
$c2 = 2 * asin(sqrt($a2));
$jobdistance = round($earthRadius * $c2,2);
/ *然后将这些距离用于进一步的比较* /