# google maps api 取兩點路線規劃的距離

```php
foreach ($shopdatalist as $key => $data) {
    $googleMapApiUrl = 'https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins='.$searchdata['sd_lat'].','.$searchdata['sd_lng'].'&destinations='.$data['sd_lat'].','.$data['sd_lng'].'&key=AIzaSyDIpQt_Dg4-4ttIIsg-RNO5VkTxxbicvk8';
    //開始call api
    $curl = curl_init();

    curl_setopt($curl, CURLOPT_URL, $googleMapApiUrl);
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);

    $strResponse = curl_exec($curl);
    curl_close($curl);
    //轉換字串為陣列
    $strResponse = json_decode($strResponse,true);
    if(isset($strResponse['rows'][0]['elements'][0]['distance']['text'])){
        $mi = $strResponse['rows'][0]['elements'][0]['distance']['text'];
        //將裡面的空白跟mi字眼拔除，只留下數字
        $mi = str_replace(' mi','',$mi);
        $mi = round($mi * 1.609,1);
        $shopdatalist[$key]['mi'] = $mi;
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://tobyisme.gitbook.io/php/google-maps-api-qu-liang-dian-lu-xian-gui-hua-de-ju-li.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
