2010-07-03 227 views
2

我正在用PHP編寫一個預訂網站,我需要一個庫或遠程服務(類似於谷歌地圖API)計算兩個地址之間的距離。兩個地址之間的距離

理想情況下,我更喜歡道路距離,但我並不在乎過多的距離。

你能幫我嗎?

非常感謝你,每一個幫助將受到歡迎。

+6

您是否有任何理由不想使用Google Maps API?它有一個距離計算器... – xil3 2010-07-03 17:55:19

+0

去谷歌地圖。這是最好的 – ggfan 2010-07-03 18:09:59

+0

我一直使用它與JavaScript,我不知道它也適用於PHP。 你知道一個很好的教程或類似的東西嗎? – TheSENDER 2010-07-04 08:05:57

回答

10

Google Maps API - Directions是一個很好的開始。

使用URL模式發送了一個請求:

http://maps.google.com/maps/api/directions/xml?origin=[FROM_ADDRESS]&destination=[TO_ADDRESS]&sensor=false 
// [FROM_ADDRESS] is a Google-Recognisable address for the Start 
// [TO_ADDRESS] is a Google-Recognisable address for the End 

爲例 - 「我怎麼到卡內基音樂廳(索尼音樂娛樂)?」

起始地址:550麥迪遜大道,新紐約,紐約州,美國 結束地址:881第7大道,紐約,紐約州,美國

從谷歌的XML路線的URL將

http://maps.google.com/maps/api/directions/xml?origin=550+Madison+Avenue,+New+York,+NY,+United+States&destination=881+7th+Avenue,+New+York,+NY,+United+States&sensor=false 

結果是:

<DirectionsResponse> 
    <status>OK</status> 
    <route> 
    <summary>E 57th St</summary> 
    <leg> 
     <step> 
     <travel_mode>DRIVING</travel_mode> 
     <start_location> 
      <lat>40.7612400</lat> 
      <lng>-73.9731300</lng> 
     </start_location> 
     <end_location> 
      <lat>40.7622900</lat> 
      <lng>-73.9723600</lng> 
     </end_location> 
     <polyline> 
      <points>wdxwF`{nbMqEyC</points> 
      <levels>BB</levels> 
     </polyline> 
     <duration> 
      <value>9</value> 
      <text>1 min</text> 
     </duration> 
     <html_instructions> 
      Head <b>northeast</b> on <b>Madison Ave</b> toward <b>E 56th St</b> 
     </html_instructions> 
     <distance> 
      <value>133</value> 
      <text>436 ft</text> 
     </distance> 
     </step> 
     <step> 
     <travel_mode>DRIVING</travel_mode> 
     <start_location> 
      <lat>40.7622900</lat> 
      <lng>-73.9723600</lng> 
     </start_location> 
     <end_location> 
      <lat>40.7655300</lat> 
      <lng>-73.9800500</lng> 
     </end_location> 
     <polyline> 
      <points>ikxwFfvnbMgS`[email protected]</points> 
      <levels>BB</levels> 
     </polyline> 
     <duration> 
      <value>148</value> 
      <text>2 mins</text> 
     </duration> 
     <html_instructions> 
      Turn <b>left</b> at the 2nd cross street onto <b>E 57th St</b> 
     </html_instructions> 
     <distance> 
      <value>741</value> 
      <text>0.5 mi</text> 
     </distance> 
     </step> 
     <step> 
     <travel_mode>DRIVING</travel_mode> 
     <start_location> 
      <lat>40.7655300</lat> 
      <lng>-73.9800500</lng> 
     </start_location> 
     <end_location> 
      <lat>40.7651800</lat> 
      <lng>-73.9803000</lng> 
     </end_location> 
     <polyline> 
      <points>[email protected]</points> 
      <levels>BB</levels> 
     </polyline> 
     <duration> 
      <value>39</value> 
      <text>1 min</text> 
     </duration> 
     <html_instructions> 
      Turn <b>left</b> at the 3rd cross street onto <b>7th Ave</b> <div style="font-size:0.9em">Destination will be on the left</div> 
     </html_instructions> 
     <distance> 
      <value>45</value> 
      <text>148 ft</text> 
     </distance> 
     </step> 
     <duration> 
     <value>196</value> 
     <text>3 mins</text> 
     </duration> 
     <distance> 
     <value>919</value> 
     <text>0.6 mi</text> 
     </distance> 
     <start_location> 
     <lat>40.7612400</lat> 
     <lng>-73.9731300</lng> 
     </start_location> 
     <end_location> 
     <lat>40.7651800</lat> 
     <lng>-73.9803000</lng> 
     </end_location> 
     <start_address>550 Madison Ave, New York, NY 10022, USA</start_address> 
     <end_address>881 7th Ave, New York, NY 10019, USA</end_address> 
    </leg> 
    <copyrights>Map data ©2010 Google, Sanborn</copyrights> 
    <overview_polyline> 
     <points>wdxwF`{nbMqEyCgS`[email protected]@</points> 
     <levels>[email protected]?B</levels> 
    </overview_polyline> 
    </route> 
</DirectionsResponse> 

所以,這兩點之間最快的路線將有細節:在秒

  • 時間

    DirectionsResponse>路徑>腿>持續時間>值

  • 純文本的持續時間

    DirectionsResponse>路線>腿>持續時間>文本

  • 距離在局部測量(英尺或米)

    DirectionsResponse>路線>腿>距離>值

  • 距離在基本單元本地測量的純文本(英里或千米)

    DirectionsResponse> route> leg> distance> text

+0

這正是我在找的東西。 謝謝你很多盧卡斯。 – TheSENDER 2010-07-04 13:42:52

+1

不客氣。任何機會,你可以標記這個答案是正確的,所以這個問題關閉了嗎?我會很感激。 – 2010-07-04 16:11:50

+0

請注意,Google現在已更改其政策,因此您必須將您的數據與Google地圖結合使用:「Distance Matrix API的使用必須與Google Map上的顯示信息相關;例如,在地圖上請求並顯示這些目的地之前,將它們分配給彼此在特定駕駛時間內的起點 - 目的地對。禁止在不顯示Google地圖的應用程序中使用該服務。# – grokpot 2015-07-08 19:06:58