1

这是我发现的一个随机地址不起作用,但该地址仍然存在。

"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

4

1 回答 1

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 'ed 在你的getLocation函数中的地址

于 2012-11-20T12:09:46.927 回答