2010-07-03 225 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