表单上的地理编码提交

Jer*_*emy 0 php sql forms submit geocode

我搜索过一些支持论坛,但仍然无法找到我正在寻找的答案.基本上,我在我的网站上有一个用户注册表.当用户单击"提交"时,它会将其数据保存在mySQL数据库中,并将其重定向到另一个页面.很简单.

但是我希望有另一个页面,其中有一个带有标记的地图,用于每个注册用户的大致位置(城市,州,国家).

如果要求用户输入自己的经度和经度,这并不难.但谁拥有那些信息随时可用???

当用户点击"提交"时,三个输入字段被合并为一个变量($ address).我怎样才能对该变量进行地理编码($ address),并将其输出到另外两个变量($ lat和$ lng)?希望在mysql_connect之前执行此操作,这样我就可以准备好插入到mySQL数据库表中.


$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$status=$_POST['status'];
$service=$_POST['service'];
$rank=$_POST['rank'];
$specialty=$_POST['specialty'];
$address=$_POST['city'] . ',' . $_POST['state'] . ',' . $_POST['country'];
$city=$_POST['city'];
$state=$_POST['state'];
$country=$_POST['country'];
$email=$_POST['email'];

mysql_connect($host,$username,$password) or die("Unable to connect to database"); @mysql_select_db($database) or die("Unable to select database");

$query = "INSERT INTO $table VALUES ('','$first_name','$last_name','$status','$service','$rank','$speciality','$address','$city','$state','$country','','','$email')"; mysql_query($query);

任何帮助将非常感激!!!运行PHP和mySQL.

Bra*_*rad 8

您可以使用Google的地理编码API.这在PHP中相当简单.使用您需要的参数在API URL上执行file_get_contents()(doc),然后在结果数据上执行json_decode()(doc).

请注意,他们的TOS表示此数据最终必须显示在Google地图上.否则,您将需要使用其他地理编码服务.

最后,不要忘记错误捕获,处理超时和其他好东西.您不希望无限期地等待Google向您回复数据.虽然他们的服务非常快,但确实会出现连接问题和其他问题.

编辑:显然除了上面的解释之外还需要更多的帮助,所以这里有一些快速的代码可以帮助你.

<?php
$address="1600 Amphitheatre Pkwy, Mountain View, CA";
$result=file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=" . urlencode($address) );
$geocodedinfo=json_decode($result);

print_r($geocodedinfo);
?>
Run Code Online (Sandbox Code Playgroud)

同样,这只是为了让你开始.在输出中,您将看到已解码的所有内容.您正在寻找的应该是:

$geocodedinfo['results'][0]['geometry']['location']['lat']
$geocodedinfo['results'][0]['geometry']['location']['lng']
Run Code Online (Sandbox Code Playgroud)