两个地址之间的距离

时间:2010-07-03 17:51:17

标签: php geolocation

我正在用PHP编写预订网站,我需要一个库或远程服务(类似于google maps api)来计算2个地址之间的距离。

理想情况下,我更喜欢道路距离,但我不太关心距离是多少。

你能帮助我吗?

非常感谢,欢迎各方帮助。

1 个答案:

答案 0 :(得分:10)

Google Maps API - Directions是一个很好的起点。

使用网址格式发送请求:

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 Madison Avenue,New York,NY,United States 完地址:881 7th Avenue,New York,NY,United States

来自Google的XML指示的网址为

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&gt;路线&gt;腿&gt;持续时间&gt;值

  • 纯文本的持续时间

    DirectionsResponse&gt;路线&gt;腿&gt;持续时间&gt;文本

  • 本地测量基准单位的距离(英尺或米)

    DirectionsResponse&gt;路线&gt;腿&gt;距离&gt;值

  • 本地测量的纯文本距离(英里或公里)

    DirectionsResponse&gt;路线&gt;腿&gt;距离&gt;文本