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