如何通过php找到两个地址之间的距离

时间:2012-09-22 05:27:39

标签: php distance

我正在寻找一些帮助,通过PHP找到两个地址之间的距离 因为我不能使用谷歌地图API。所以从这篇文章中获取方法

Distance between two addresses

但我需要知道如何使用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

感谢任何建议。

///////////////

在这些答案后,我在那里

     $customer_address_url =  urlencode($customer_address);
     $merchant_address_url =  urlencode($merchant_address);

     $map_url = "http://maps.google.com/maps/api/directions/xml?origin=".$merchant_address_url."&destination=".$customer_address_url."&sensor=false";

      $response_xml_data = file_get_contents($map_url);
      $data = simplexml_load_string($response_xml_data);

当我输入此网址时,可以看到XML响应:http://maps.google.com/maps/api/directions/xml?origin=Quentin+Road+Brooklyn%2C+New+York%2C+11234+United+States&destination=550+Madison+Avenue+New+York%2C+New+York%2C+10001+United+States&sensor=false

但无法通过

打印
      echo "<pre>"; print_r($data); exit;  

2 个答案:

答案 0 :(得分:0)

你可以试试这个..

$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";
$data = file_get_contents($url);

现在$data包含生成的xml数据。您可以使用..

解析此xml数据

http://php.net/manual/simplexml.examples.php

答案 1 :(得分:0)