谷歌地理编码不适用于数据库中带有特殊字符的地址 [英] google geocode not working for addresses with special characters from database

查看:108
本文介绍了谷歌地理编码不适用于数据库中带有特殊字符的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Google地理编码的数据库地址中存在特殊字符的问题,但是如果我对它们进行硬编码则没有问题.

I'm having an issue with special characters for addresses from my database for google geocode, but not if I hardcode them.

简单的地理代码

$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;

此代码适用于标准地址,但如果该地址来自我的数据库,则该地址中包含特殊字符时将无效.

This code works for standard addresses but doesn't work when the address has a special character in it IF it comes from my database.

如果我将地址硬编码为:

If I hardcode the address as:

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

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

The code works. If the address comes from my database

$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无效.

Database is in utf8_unicode_ci. If I echo the $url including the address even if it has come from my database, and then put that url into a browser the url actually spits out the right data but using that url in my code doesn't work.

我尝试对str_replace之前和str_replace之后的地址进行网址编码,但仍然无法正常工作.

I've tried to url encode the address before the str_replace and also after the str_replace and it still doesn't work.

我如何使它工作?

推荐答案

该怎么办,请执行以下操作:

What if, you do the following:

$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...
}

这篇关于谷歌地理编码不适用于数据库中带有特殊字符的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆