Php 谷歌地图地理编码不返回结果
这是一个我发现不起作用的随机地址,但该地址仍然存在 英国泰恩和威尔郡盖茨黑德团队谷贸易区NE92 1DG 你们能试着用地理编码告诉我它对你们有用吗,我有13个地址,12个有效,这1个不行 这是我正在运行以发出地理代码请求的代码:Php 谷歌地图地理编码不返回结果,php,google-maps,geocoding,Php,Google Maps,Geocoding,这是一个我发现不起作用的随机地址,但该地址仍然存在 英国泰恩和威尔郡盖茨黑德团队谷贸易区NE92 1DG 你们能试着用地理编码告诉我它对你们有用吗,我有13个地址,12个有效,这1个不行 这是我正在运行以发出地理代码请求的代码: class geocoder{ static private $url = "http://maps.google.com/maps/api/geocode/json?sensor=true&address="; static public f
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);
这就是地址存在的证明:
这证明地理编码类应该返回数据:
您正在两次输入该地址,因此请更改:
$address = urlencode("Team Valley Trading Estate, Gateshead, Tyne and Wear NE92 1DG, UK");
到
由于您已在
getLocation
函数中对地址进行了urlencod,我正在对url进行双端编码…****羞愧地低头。noobs就是noobs。我刚刚在我的网站原型上试用过,地址工作得很好,正确返回坐标、行政区域和2级(省/县)和邮政编码,所以问题应该在你的脚本中,可能是urlencode弄糟了,我不知道。我可以链接我的网站,这样你就可以看到我的代码(虽然它不是面向对象的,而且比你的imo更基本/简单/更糟糕,但它是xD)如果你请求的话,我不想把url放在第一位,因为它看起来像垃圾邮件xD当地理编码时,我总是试图从字符串的开头(国家、地区、街道名称、街道号码)获取最重要的信息,如果找不到任何内容,我将删除字符串的最后一个单词,然后重试。
$address = "Team Valley Trading Estate, Gateshead, Tyne and Wear NE92 1DG, UK";