谷歌地图编码不返回结果

时间:2012-11-20 11:58:58

标签: php google-maps geocoding

这是我发现的随机地址不起作用,但地址存在。

“Team Valley Trading Estate,Gateshead,Tyne and Wear NE92 1DG,UK”

你们可以试试地理编码并告诉我它是否适合你,我有13个地址,12个工作,这1个不工作?

这是我为运行地理编码请求而运行的代码:

class geocoder{
    static private $url = "http://maps.google.com/maps/api/geocode/json?sensor=true&address=";

    static public function getLocation($address){
        $url = self::$url.urlencode($address);

        $resp_json = self::curl_file_get_contents($url);
        $resp = json_decode($resp_json, true);

        if($resp['status']='OK'){
            return $resp['results'][0]['geometry']['location'];
        }else{
            return false;
        }
    }

    static private function curl_file_get_contents($URL){
        $c = curl_init();
        curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($c, CURLOPT_URL, $URL);
        $contents = curl_exec($c);
        curl_close($c);

        if ($contents) return $contents;
            else return FALSE;
    }
}    


$address = urlencode("Team Valley Trading Estate, Gateshead, Tyne and Wear NE92 1DG, UK");
$loc = geocoder::getLocation($address);

这证明地址存在: http://maps.google.co.uk/maps?q=%22Team+Valley+Trading+Estate,+Gateshead,+Tyne+and+Wear+NE92+1DG,+UK%22&hl=en&sll=53.103814,-2.278518&sspn=10.722701,33.815918&t=h&hnear=NE92+1DG,+United+Kingdom&z=16

这证明了地理编码类应该返回数据:

http://maps.google.com/maps/api/geocode/json?sensor=true&address=Team%20Valley%20Trading%20Estate,%20Gateshead,%20Tyne%20and%20Wear%20NE92%201DG,%20UK

1 个答案:

答案 0 :(得分:2)

urlencod 两次地址,所以改变:

$address = urlencode("Team Valley Trading Estate, Gateshead, Tyne and Wear NE92 1DG, UK");

$address = "Team Valley Trading Estate, Gateshead, Tyne and Wear NE92 1DG, UK";

因为你有 urlencod 'getLocation函数

中的地址