【发布时间】:2012-01-06 06:26:17
【问题描述】:
我在使用地理编码器和返回正确的纬度/经度坐标时遇到问题。
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。
谁能帮我提供正确的代码来获取带有国际字符的街道名称的纬度/语言地址?
【问题讨论】:
-
1.摆脱所有
htmlentities()的东西。 2.你的PHP文件是什么字符编码的?如果是 UTF-8,它应该可以工作 - 我会先尝试 -
1.完毕。没有变化。我还删除了
html_entity_decode- 仍然没有变化。 2. UTF-8。但是“现实生活中”的地址是从 DB 中检索到的,并且表在DEFAULT CHARSET=utf8 COLLATE=utf8_danish_ci中。
标签: php google-maps-api-3 geocoding