簡體   English   中英

使用Google Maps API計算兩個地址之間的距離

[英]Using Google Maps API to calculate distance between two addresses

背景信息:由於我屬於NDA,因此我不能透露太多有關該項目的信息。

我正在使用本教程來使用Google Maps計算兩個地址之間的距離。

我希望輸出的是定點的起始地址與目標地址之間的距離,該目標地址取決於用戶在_ct_text_53ea3270e6e6d字段中輸入的_ct_text_53ea3270e6e6d

從一個地址拉出它可以正常工作,但是如果從多個地圖中拉出它,則僅顯示到其中一個地圖的距離,而不是每個地圖的距離。

這是顯示問題的屏幕截圖:

在此處輸入圖片說明

這是我的代碼:

$field_value = do_shortcode('[ct id="_ct_text_53ea3270e6e6d" property="title | description | value"]'); 

// Our parameters
$params = array(
    'origin'        => '900 South Crouse Ave, Syracuse, NY, 13210',
    'destination'   => $field_value,
    'sensor'        => 'true',
    'units'         => 'imperial'
);

// Join parameters into URL string
foreach($params as $var => $val){
    $params_string .= '&' . $var . '=' . urlencode($val);  
}

// Request URL
$url = "http://maps.googleapis.com/maps/api/directions/json?".ltrim($params_string, '&');

// Make our API request
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$return = curl_exec($curl);
curl_close($curl);

// Parse the JSON response
$directions = json_decode($return);

// Print distance
 print('<p><strong>Distance from Campus: </strong>' .$directions->routes[0]->legs[0]->distance->text. '</p>');  

不得不替換為:

// Join parameters into URL string
foreach($params as $var => $val){
    $params_string .= '&' . $var . '=' . urlencode($val);
}

附:

$params_string = http_build_query($params);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM