亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Laravel獲取一個位置和位置數組之間的距離谷歌矩陣

Laravel獲取一個位置和位置數組之間的距離谷歌矩陣

PHP
白豬掌柜的 2023-07-01 13:00:42
我多次問過這個問題但沒有答案,我想使用谷歌的matrixapi獲取客戶和50家商店之間的駕駛距離,其中我有客戶的緯度和經度,以及每個商店的經緯度數組ID       array:57 [▼      0 => array:3 [▼        "id" => 695        "lat" => "2.9315257"        "long" => "101.7638137"      ]      1 => array:3 [▼        "id" => 696        "lat" => "2.9660291"        "long" => "101.7499998"      ]      2 => array:3 [▼        "id" => 700        "lat" => "2.937143099999999"        "long" => "101.7779784"      ]我使用了 url,但很難使用它 forloop 那么我如何傳遞數組并獲取每個商店及其 id 的距離     $distance_data = file_get_contents(                'https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins='.$from_latlong.'&destinations='.$to_latlong.'&key='.$googleApi            );                     $distance_arr = json_decode($distance_data);            $distance = $distance_arr->rows[0]->elements[0]->distance->text;            $duration = $distance_arr->rows[0]->elements[0]->duration->text;                       $distance = preg_replace("/[^0-9.]/", "",  $distance);            $duration = preg_replace("/[^0-9.]/", "",  $duration);                       $distance=$distance * 1.609344;                       $distance=number_format($distance, 1, '.', '');            $duration=number_format($duration, 1, '.', '');     
查看完整描述

1 回答

?
神不在的星期二

TA貢獻1963條經驗 獲得超6個贊

我還沒有嘗試運行它,但從文檔來看,您似乎應該用|.

起點- 計算行駛距離和時間的起點。您可以以地址、緯度/經度坐標或地點 ID 的形式提供一個或多個由豎線字符 (|) 分隔的位置:

目的地— 一個或多個位置,用作計算行駛距離和時間的終點。目的地參數的選項?與起源參數的選項相同,如上所述

因此,此代碼循環遍歷目的地并使用值創建第二個數組x,y,然后implode()使用|...

$dests = [[

? ? ? ? "id" => 695,

? ? ? ? "lat" => "2.9315257",

? ? ? ? "long" => "101.7638137"

? ? ? ? ],

? ? ? ? [

? ? ? ? "id" => 696,

? ? ? ? "lat" => "2.9660291",

? ? ? ? "long" => "101.7499998"

? ? ? ? ],

? ? ? ? [

? ? ? ? "id" => 700,

? ? ? ? "lat" => "2.937143099999999",

? ? ? ? "long" => "101.7779784"

]];


$coords = array_map(function($data) { return? $data['lat'].",".$data['long']; }, $dests );

$from_latlong = implode("|", $coords);

$distance_data = file_get_contents(

? ? ? ? 'https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins='.$from_latlong.'&destinations='.$to_latlong.'&key='.$googleApi

? ? ? ? );

所以目的地看起來像......


2.9315257,101.7638137|2.9660291,101.7499998|2.937143099999999,101.7779784

編輯


從您發布的額外代碼來看,它會是這樣的......


$distance_arr = json_decode($distance_data);

foreach ( $distance_arr->rows[0] as $key => $element )? {

? ? $distance = $element->distance->text;

? ? $duration = $element->duration->text;

? ? // The matching ID

? ? $id = $dests[$key];

? ??

? ? $distance = preg_replace("/[^0-9.]/", "",? $distance);

? ? $duration = preg_replace("/[^0-9.]/", "",? $duration);

? ??

? ? $distance=$distance * 1.609344;


? ? $distance=number_format($distance, 1, '.', '');

? ? $duration=number_format($duration, 1, '.', '');??

}

將foreach循環遍歷元素,然后$key將是元素的索引以及目標,因此您可以從源數組中的匹配元素中挑選出 ID。


查看完整回答
反對 回復 2023-07-01
  • 1 回答
  • 0 關注
  • 125 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號