【问题标题】:google geocode not working for addresses with special characters from database谷歌地理编码不适用于数据库中具有特殊字符的地址
【发布时间】:2013-09-25 14:48:19
【问题描述】:

我的数据库中用于 google 地理编码的地址的特殊字符存在问题,但如果我对它们进行硬编码,则不会。

简单的地理编码

$url = "http://maps.googleapis.com/maps/api/geocode/json?address=". $address . "&sensor=true";      
$jsonfile = file_get_contents($url);
$jsondata = json_decode($jsonfile);
$lat = $jsondata->results[0]->geometry->location->lat;
$lng = $jsondata->results[0]->geometry->location->lng;

此代码适用于标准地址,但如果地址中包含特殊字符(如果它来自我的数据库)则不起作用。

如果我将地址硬编码为:

$address = "10 Montée de Clausen, 1343 Luxemburg City, Luxembourg";
$address = str_replace(" ","+",$address);

代码有效。 如果地址来自我的数据库

$query="SELECT * FROM mytable";
$result=mysqli_query($GLOBALS["___mysqli_ston"], $query);
while($row=mysqli_fetch_assoc($result)){
    $address = $row['address'];
    $address = str_replace(" ","+",$address);
    // geocode code.
}

它不起作用。

数据库位于 utf8_unicode_ci 中。 如果我回显包含地址的 $url,即使它来自我的数据库,然后将该 url 放入浏览器中,该 url 实际上会吐出正确的数据,但在我的代码中使用该 url 不起作用。

我尝试在 str_replace 之前和 str_replace 之后对地址进行 url 编码,但它仍然不起作用。

如何让它工作?

【问题讨论】:

标签: php mysql json geocoding


【解决方案1】:

如果,您执行以下操作:

$query="SELECT * FROM mytable";
$result=mysqli_query($GLOBALS["___mysqli_ston"], $query);
while($row=mysqli_fetch_assoc($result)){
    $address = urlencode(mb_convert_encoding($row['address'], 'UTF-8'));
    // geocode code...
}

【讨论】:

  • 非常感谢。我认为它与编码有关,但不知道如何修复它。再次感谢!
【解决方案2】:

数据库输出数据试试urlencode

喜欢

 $address = urlencode($row['address'])

同时从当前代码中删除这个$address = str_replace(" ","+",$address);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-13
    • 1970-01-01
    • 2019-06-10
    相关资源
    最近更新 更多