doucheng5209 2014-04-24 06:46
浏览 41
已采纳

计算php中两个邮政编码之间的距离[关闭]

$url =
"http://maps.googleapis.com/maps/api/distancematrix/json?origins=144216&
destinations=160017&mode=driving&language=en-EN&sensor=false";
   $data   = @file_get_contents($url);
   $result = json_decode($data, true); //print_r($result);  //outputs the array    $distances = array( // converts the units
   "meters" => $result["rows"][0]["elements"][0]["distance"]["value"],
   "kilometers" => $result["rows"][0]["elements"][0]["distance"]["value"] / 1000,
   "yards" => $result["rows"][0]["elements"][0]["distance"]["value"] * 1.0936133,
   "miles" => $result["rows"][0]["elements"][0]["distance"]["value"] * 0.000621371    );        
print_r($distances);

It is showing this output:

Array ( 
  [meters] => 5497949 
  [kilometers] => 5497.949 
  [yards] => 6012630.1491217 
  [miles] => 3416.266068079
)

which is not correct. Please let me know any solution

  • 写回答

1条回答 默认 最新

  • dongmi4035 2014-04-24 06:58
    关注

    If you take a look at the result from google you will notice that it is calculating a route from india to russia.

    "destination_addresses" : [ "Wologda, Oblast Wologda, Russland, 160017" ],
    "origin_addresses" : [ "Punjab 144216, Indien" ],
    

    This happens because you only provided postal codes, without reference from which country they are. I assume you want to calculate a route within india, since your profile shows that you are from there.

    If you just add ,india to your postal codes, like this:

    http://maps.googleapis.com/maps/api/distancematrix/json?origins=144216,india&destinations=160017,india&mode=driving&language=en-EN&sensor=false
    

    You get a result that looks much more like what you want:

    {
       "destination_addresses" : [ "Chandigarh, 160017, India" ],
       "origin_addresses" : [ "Punjab 144216, India" ],
       "rows" : [
          {
             "elements" : [
                {
                   "distance" : {
                      "text" : "201 km",
                      "value" : 200693
                   },
                   "duration" : {
                      "text" : "3 hours 30 mins",
                      "value" : 12625
                   },
                   "status" : "OK"
                }
             ]
          }
       ],
       "status" : "OK"
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案