表单提交时进行地理编码

发布于 2024-10-02 12:46:51 字数 1080 浏览 9 评论 0原文

我搜索了许多支持论坛,但仍然找不到我正在寻找的答案。本质上,我的网站上有一个用户注册表。当用户单击“提交”时,它会将数据保存在 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("无法连接到数据库"); @mysql_select_db($database) 或 die("无法选择数据库");

$query = "INSERT INTO $table VALUES ('','$first_name','$last_name','$status','$service','$rank','$speciality','$address','$城市','$州','$国家','','','$电子邮件')"; mysql_query($查询);

任何帮助将不胜感激!使用 PHP 和 mySQL 运行。

I've searched through a number of support forums and I still can't find the answer I'm looking for. Essentially, I have a user registration form on my website. When the user clicks "submit" it saves their data in a mySQL database and redirects them to another page. Simple enough.

But I want to have another page that has a map with a marker for each registered users' approximate location (city, state, country).

This isn't too hard to do if users were required to input their own lattitude and longitude. But who has that information readily available???

When the user clicks "submit" three input fields are combined into one variable ($address). How can I geocode that variable ($address), and have it output to two other variables ($lat and $lng)??? Looking to do this BEFORE mysql_connect, that way I have everything ready to insert into mySQL database table.


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

Any help would be MUCH appreciated!!! Running with PHP and mySQL.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

机场等船 2024-10-09 12:46:51

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

请注意,他们的服务条款规定这些数据最终必须显示在谷歌地图上。否则,您将需要使用其他地理编码服务。

最后,不要忘记错误捕获、处理超时和其他好处。您不想无限期地等待 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);
?>

再次强调,这只是为了让您开始。在输出中,您将看到已解码的所有内容。您要寻找的内容应该在这些中:

$geocodedinfo['results'][0]['geometry']['location']['lat']
$geocodedinfo['results'][0]['geometry']['location']['lng']

You can use Google's Geocoding API. This is fairly straightforward in PHP. Do a file_get_contents() (doc) on the API URL with the parameters you need, and then a json_decode() (doc) on the resulting data.

Note that their TOS says this data must eventually be displayed on a Google Map. Otherwise you will need to use another Geocoding service.

Finally, don't forget error trapping, handling timeouts, and other goodies. You don't want to wait indefinitely for Google to reply to you with data. While their service is very fast, connection troubles and other issues do arise.

Edit: Apparently more help is needed beyond my explanation above, so here is some quick code to help you.

<?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);
?>

Again, this is just to get you started. In the output, you will see everything that was decoded. What you are looking for should be in these:

$geocodedinfo['results'][0]['geometry']['location']['lat']
$geocodedinfo['results'][0]['geometry']['location']['lng']
淡水深流 2024-10-09 12:46:51

使用 Yahoo! Placefinder 或其他地理编码 API 之一。

Use Yahoo! Placefinder or one of the other geocoding APIs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文