簡體   English   中英

使用PHP的Google Maps API查找兩個位置之間的距離

[英]Google Maps API with PHP to find distance between two locations

在我當前的項目,即交付系統,我有一個可用的交付驅動程序列表,顯示在訂單頁面上。 但我需要的是顯示每個交付距離客戶地址的距離。 應在每個駕駛員姓名旁邊顯示距離。 任何人都有任何線索我會如何解決這個問題?

假設您需要行駛距離而不是直線距離,則可以使用Directions Web服務:您需要從PHP腳本發出http或https請求並解析JSON或XML響應。

文檔: https//developers.google.com/maps/documentation/directions/

例如:波士頓,MA到Concord,MA通過Charlestown,MA和Lexington,MA(JSON) http://maps.googleapis.com/maps/api/directions/json?origin=Boston,MA&destination=Concord,MA&waypoints=Charlestown ,MA |列克星敦,MA&傳感器=假

馬薩諸塞州波士頓,馬薩諸塞州康科德市,馬薩諸塞州查爾斯頓市和馬薩諸塞州列克星敦市(XML) http://maps.googleapis.com/maps/api/directions/xml?origin =波士頓,馬薩諸塞州和目的地= Concord,MA&waypoints = Charlestown,MA |列克星敦,MA&傳感器=假

請注意,有使用限制。

您可以使用Google Maps API和PHP輕松計算兩個地址之間的距離。

$addressFrom = 'Insert from address';
$addressTo = 'Insert to address';
$distance = getDistance($addressFrom, $addressTo, "K");
echo $distance;

您可以從這里獲取getDistance()函數代碼 - http://www.codexworld.com/distance-between-two-addresses-google-maps-api-php/

這將在2個位置之間的秒數

$origin =urlencode('Optus Sydney International Airport, Airport Drive, Mascot NSW 2020, Australia');
$destination = urlencode('Sydney NSW, Australia');
$url = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=$origin&destinations=$destination&key=your_key";
$data = @file_get_contents($url);
$data = json_decode($data,true);
echo $data['rows'][0]['elements'][0]['duration']['value'];

暫無
暫無

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

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