doucheng5209
doucheng5209
2014-04-24 06:46

计算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 dongmi4035 7年前

    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"
    }
    
    点赞 评论 复制链接分享

相关推荐