两个地址之间的距离

3
我正在使用PHP编写一个预订网站,需要一个类库或远程服务(类似于Google Maps API)来计算两个地址之间的距离。最好是路程距离,但我不太在意是什么类型的距离。你能帮我吗?非常感谢,每一次帮助都会受到欢迎。

6
您不想使用Google Maps API的原因是什么?它有一个距离计算器... - xil3
使用谷歌地图,它是最好的。 - ggfan
我一直在使用JavaScript,不知道它也可以与PHP一起使用。你知道有什么好的教程或类似的东西吗? - TheSENDER
1个回答

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

示例 - "如何从Sony音乐娱乐公司到达卡内基音乐厅?"

起始地址:美国纽约州纽约市麦迪逊大道550号 目的地地址:美国纽约州纽约市第七大道881号

谷歌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`o@</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>q_ywFhfpbMdAp@</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`o@dAp@</points>
      <levels>B@?B</levels>
    </overview_polyline>
  </route>
</DirectionsResponse>

因此,这两点之间最快的路线将包括以下细节:

  • 耗时(以秒为单位)

    DirectionsResponse > route > leg > duration > value

  • 耗时(以常规文字方式显示)

    DirectionsResponse > route > leg > duration > text

  • 距离(以当地度量单位的基本单位,如英尺或米,表示)

    DirectionsResponse > route > leg > distance > value

  • 距离(以当地度量单位的常规文字方式,如英里或千米,表示)

    DirectionsResponse > route > leg > distance > text


这正是我正在寻找的。非常感谢你,Lucanos。 - TheSENDER
1
不客气。您能否将此答案标记为正确,这样问题就可以关闭了吗?我会非常感激的。 - Luke Stevenson
请注意,谷歌现已更改其政策,因此您必须将数据与谷歌地图结合使用:使用距离矩阵 API 必须与在谷歌地图上显示信息相关;例如,在请求和显示地图上的目的地之前,起点-终点对必须在特定驾驶时间内相互匹配。在不显示谷歌地图的应用程序中使用该服务是被禁止的。 - grokpot
感谢您提供的信息,@praterade。希望我提供的解决方案能够与Google地图结合使用。 - Luke Stevenson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接