将地址转换为坐标

时间:2014-04-15 13:39:24

标签: php google-maps google-maps-api-3

我正在尝试将地址转换为geocoordiantes。当我调用此函数并打印latlong值系统时,给我以下错误。

  

警告:file_get_contents(http://maps.googleapis.com/maps/api/geocode/json?latlng=Raiwind Road,Lahore,Pakistan,74.2306& sensor = false)[function.file-get-contents]:无法打开流:HTTP请求失败!第137行的C:\ wamp \ www \ temp \ yy.php中的HTTP / 1.0 400错误请求

代码如下:请妥善解决

      function getlatlong($address)  
          {   
      $Address = urlencode($address);
      $request_url = "http://maps.googleapis.com/maps/api/geocode/xml?address=".$Address."&sensor=true";
      $xml = simplexml_load_file($request_url) or die("url not loading");
       $status = $xml->status;
       if ($status=="OK") {
         $Lat = $xml->result->geometry->location->lat;
        $Lon = $xml->result->geometry->location->lng;
         $LatLng = "$Lat,$Lon";
          }
           return $LatLng;
              }
            $coord=getlatlong($source1);
             print $coord;

1 个答案:

答案 0 :(得分:1)

这对我有用:

<?php
  $Address = urlencode($Address);
  $request_url = "http://maps.googleapis.com/maps/api/geocode/xml?address=".$Address."&sensor=true";
  $xml = simplexml_load_file($request_url) or die("url not loading");
  $status = $xml->status;
  if ($status=="OK") {
      $Lat = $xml->result->geometry->location->lat;
      $Lon = $xml->result->geometry->location->lng;
      $LatLng = "$Lat,$Lon";
  }
?>

参考文献: