/**
* Notes:地理编码--通过地址获取经纬度
* User: 任性不起来了
* Date: 2024/8/27 9:36
* @param $data
* @param $user
*/
public function geocode($data,$user){
$start_address = $data['start_address'] ;
$end_address = $data['end_address'] ;
$start = geocode($start_address);
$end = geocode($end_address);
$log = [
'start' => $start ,
'end' => $end ,
] ;
error_log(date('Y-m-d H:i:s') . ' 地址信息-----' . json_encode($log, 320) . ' ' . PHP_EOL, 3, '../runtime/googlemap.log');
if(!$start || !$end){
return ApiReturn::r(0, [], '地址解析失败');
}
//根据经纬度获取距离
$distance = getRouteDistance($start['lng'], $start['lat'], $end['lng'], $end['lat'] ,2);
error_log(date('Y-m-d H:i:s') . ' 距离-----' . json_encode($distance, 320) . ' ' . PHP_EOL, 3, '../runtime/googlemap.log');
$data = [
'is_once' => 1,
'distance' => $distance,
] ;
$info = Deliver::get_pt_freight($start, $end, $data);
$info['distance'] = $distance ;
$info['unit'] = 'km' ;
error_log(date('Y-m-d H:i:s') . ' 查询派送距离与派送费用接口-----' . json_encode($info, 320) . ' ' . PHP_EOL, 3, '../runtime/googlemap.log');
return ApiReturn::r(1, $info, '查询派送距离与派送费用接口');
}
/**
* 地理编码--通过地址获取经纬度
*/
if (!function_exists("geocode")){
/**
* Notes:地理编码--通过地址获取经纬度
* User: 任性不起来了
* Date: 2024/8/27 9:36
* @param $address
*/
function geocode($address){
$key = config('googlemap_key') ;
// $address = '1600+Amphitheatre+Parkway,+Mountain+View,+CA';
$address = urlencode($address) ; //此处需进行地址编码
$url = "https://maps.googleapis.com/maps/api/geocode/json?address={$address}&key={$key}" ;
$res = file_get_contents($url);
$res = json_decode($res, true);
// error_log(date('Y-m-d H:i:s') . ' 地理编码--通过地址获取经纬度-----' . json_encode($res, 320) . ' ' . PHP_EOL, 3, '../runtime/googlemap.log');
if (!empty($res) && $res['status'] == 'OK' && $res['results'][0]['geometry']['location']) {
//经纬度信息
$location = $res['results'][0]['geometry']['location'] ;
return ['lat'=>$location['lat'],'lng'=>$location['lng']] ;
}
return [] ;
}
}
/**
* 获取路线规划距离
*/
if (!function_exists('getRouteDistance')){
/**
* Notes:根据googl地图获取路线距离
* User: 任性不起来了
* Date: 2024/8/27 9:36
* @param Decimal $longitude1 起点经度
* @param Decimal $latitude1 起点纬度
* @param Decimal $longitude2 终点经度
* @param Decimal $latitude2 终点纬度
* @param Int $decimal 精度 保留小数位数
* @return Decimal
*/
function getRouteDistance($longitude1, $latitude1, $longitude2, $latitude2 ,$decimal=2){
$key = config('googlemap_key') ;
$unit = "metric"; //单位 metric:米/公里
$url = "https://maps.googleapis.com/maps/api/distancematrix/json?units={$unit}&key={$key}&origins={$latitude1},{$longitude1}&destinations={$latitude2},{$longitude2}";
$url = urldecode($url);
$result = file_get_contents($url);
$result = json_decode($result,true);
if($result['status'] != 'OK'){
return false;
}
if(!isset($result['rows']) || !isset($result['rows'][0]['elements'][0]['distance']['text'])){
return false;
}
$distance_text = $result['rows'][0]['elements'][0]['distance']['text'];
$distance = $result['rows'][0]['elements'][0]['distance']['value'];
$place = stristr($distance_text,'km');
// $duration_text 1 hour 58 mins 或 2 min
if($place){
if($distance <=0){
$distance = 1;
}
$distance = $distance / 1000;
}
$distance = round($distance, $decimal);
if($distance <=0){
$distance = 1;
}
return $distance;
}
}
03-06