谷歌地图地理编码器未返回正确的地理位置
我在使用地理编码器和返回正确的纬度/经度坐标时遇到问题。
controller
地址返回正确的地理位置,但是当我在地址中使用字符 ø
时,我得到了一些奇怪的坐标。
这是我的代码:
$controller = "Skovveien+7+0257+oslo+norway";
// base = Strømmen Storsenter, Støperiveien 5, 2010 Strømmen, Norway
$t1 = htmlentities("Strømmen+Storsenter+2010+strømmen+norway", ENT_QUOTES, 'UTF-8');
$t2 = htmlentities("Støperiveien+5+2010+strømmen+norway", ENT_QUOTES, 'UTF-8');
$adr = htmlentities($controller, ENT_QUOTES, 'UTF-8');
$res = getGeoLocation($adr);
echo '<p>Controll<br/> Lat: '.$res['lat'].' Lng: '.$res['lng']. '<p/>';
$adr = htmlentities($t1, ENT_QUOTES, 'UTF-8');
$res = getGeoLocation($adr);
echo '<p>Shopping mall<br/> Lat: '.$res['lat'].' Lng: '.$res['lng']. '<p/>';
$adr = htmlentities($t2, ENT_QUOTES, 'UTF-8');
$res = getGeoLocation($adr);
echo '<p>Adresse + post code<br/> Lat: '.$res['lat'].' Lng: '.$res['lng']. '<p/>';
public function getGeoLocation($adr) {
// Decode special chars
$adr = html_entity_decode($adr, ENT_QUOTES, 'UTF-8');
$url = "http://maps.google.com/maps/api/geocode/json?address=$adr&sensor=false";
$jsonData = file_get_contents($url);
$geocode = json_decode($jsonData);
if($geocode->status == 'OK') {
$geocode = $geocode->results[0];
$res['lat'] = $geocode->geometry->location->lat;
$res['lng'] = $geocode->geometry->location->lng;
return $res;
} else {
return false;
}
}
我知道首先使用 htmlentities,然后在函数中使用 html_entity_decode 似乎有点毫无意义。但如果我不这样做,Google 将返回 ZERO_RESULT
。
任何人都可以帮助我使用正确的代码来获取具有国际字符的街道名称的纬度/朗地址吗?
I'm having problems using geocoder and returning correct lat / lang coordinates.
The controller
address is returning correct geo location, but when I'm using the character ø
in the address, I'm gettin som weird coordinates.
Here is my code:
$controller = "Skovveien+7+0257+oslo+norway";
// base = Strømmen Storsenter, Støperiveien 5, 2010 Strømmen, Norway
$t1 = htmlentities("Strømmen+Storsenter+2010+strømmen+norway", ENT_QUOTES, 'UTF-8');
$t2 = htmlentities("Støperiveien+5+2010+strømmen+norway", ENT_QUOTES, 'UTF-8');
$adr = htmlentities($controller, ENT_QUOTES, 'UTF-8');
$res = getGeoLocation($adr);
echo '<p>Controll<br/> Lat: '.$res['lat'].' Lng: '.$res['lng']. '<p/>';
$adr = htmlentities($t1, ENT_QUOTES, 'UTF-8');
$res = getGeoLocation($adr);
echo '<p>Shopping mall<br/> Lat: '.$res['lat'].' Lng: '.$res['lng']. '<p/>';
$adr = htmlentities($t2, ENT_QUOTES, 'UTF-8');
$res = getGeoLocation($adr);
echo '<p>Adresse + post code<br/> Lat: '.$res['lat'].' Lng: '.$res['lng']. '<p/>';
public function getGeoLocation($adr) {
// Decode special chars
$adr = html_entity_decode($adr, ENT_QUOTES, 'UTF-8');
$url = "http://maps.google.com/maps/api/geocode/json?address=$adr&sensor=false";
$jsonData = file_get_contents($url);
$geocode = json_decode($jsonData);
if($geocode->status == 'OK') {
$geocode = $geocode->results[0];
$res['lat'] = $geocode->geometry->location->lat;
$res['lng'] = $geocode->geometry->location->lng;
return $res;
} else {
return false;
}
}
I know it seems a bit pointless first using htmlentities
, then using html_entity_decode
in the function. But if I don't, Google returns ZERO_RESULT
.
Can anyone help me with correct code for fetching lat/lang address for street names with international characters?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你的 PHP 文件是 UTF-8 编码的,这个压缩示例应该可以工作:
编辑: 奇怪的是,当我使用时,我得到一个
APPROXIMATE
地址但当我仅使用街道地址时,它是一个
ROOFTOP
:尽管如此,正如您在地图上看到的那样,Google 完全知道该特定地址存在一个名为“Strømmen Storsenter”的位置!
毕竟,这看起来不像是字符集问题,而是 Google 解析包含地名的请求的方式很奇怪。
If your PHP file is UTF-8 encoded, this condensed example should work:
Edit: Weirdly, I get an
APPROXIMATE
address when I usebut a
ROOFTOP
one when I use only the street address:Even though, as you can see on the map, Google is perfectly aware that a location named "Strømmen Storsenter" exits at that particular address!
This doesn't look like a character set issue after all, but a weirdness in the way how Google parses requests with place names in it.