Php 附近地点脚本帮助

发布于 2024-10-07 08:21:41 字数 1165 浏览 1 评论 0原文

我正在阅读 http://code.google.com/apis/maps/articles/ phpsqlsearch.html 获取 SQL/数学。我写了一些 php 脚本,但没有得到任何结果。

    <?php
$db_host = "localhost";
$db_username = "root"; 
$db_pass = "";
$db_name = "places";

mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysql_select_db("$db_name") or die ("no database");

$lat = "37";
$lon = "-122";
$radius = "25";
$sql = mysql_query("SELECT id, name, ( 3959 * acos( cos( radians('$lat') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('$lon') ) + sin( radians('$lat') ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < '$radius' ORDER BY distance LIMIT 0 , 20") or die("error");

$nearby_check = mysql_num_rows($sql); 
if ($nearby_check > 0)
{
    while($row = mysql_fetch_array($sql))
    {
        $name = $row["name"];
        echo $name . '<br />';
    }
}
else
{
    echo 'No Places found nearby';  
}
?>

数据库结构和数据与Google代码文章中的相同。

这就是我的 PHP 脚本。有什么问题吗?我认为这可能是 SQL 问题。不太确定。这篇文章是 2008 年 1 月发布的,所以内容可能发生了变化?

I was reading http://code.google.com/apis/maps/articles/phpsqlsearch.html to get the SQL/Math. I wrote a little php script but i get no results.

    <?php
$db_host = "localhost";
$db_username = "root"; 
$db_pass = "";
$db_name = "places";

mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysql_select_db("$db_name") or die ("no database");

$lat = "37";
$lon = "-122";
$radius = "25";
$sql = mysql_query("SELECT id, name, ( 3959 * acos( cos( radians('$lat') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('$lon') ) + sin( radians('$lat') ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < '$radius' ORDER BY distance LIMIT 0 , 20") or die("error");

$nearby_check = mysql_num_rows($sql); 
if ($nearby_check > 0)
{
    while($row = mysql_fetch_array($sql))
    {
        $name = $row["name"];
        echo $name . '<br />';
    }
}
else
{
    echo 'No Places found nearby';  
}
?>

Database structure and data is the same as in the Google code article.

Thats my Php script. Whats wrong with it? I am thinking it might be a SQL issue. Not really sure. This post is from Jan 2008, So maybe stuff changed?

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

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

发布评论

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

评论(2

Spring初心 2024-10-14 08:21:41

http://www.geonames.org/export/

使用 geonames.org 获得您想要的东西。那就是寻找附近的地方。

该服务器从未宕机。与 http://webservicex.net 不同,我已经检查了很长时间。

您可以在 JavaScript 和 PHP 中使用它。

http://booking.com 使用这样的服务来显示酒店附近的酒店、机场等。

我一直在我的一个项目中使用这项服务。

geonames 提供了一个数据库转储,我希望他们定期更新。

您可以在他们的网站上找到更多信息。

http://www.geonames.org/export/web-services.html

http://forum.geonames.org/gforum/posts/list/499.page

http://www.geonames.org/export/

Use geonames.org to get what you want. that is finding near by places.

This server was never down. I have been checking this for a long time unlike http://webservicex.net.

You can use it in javascript as well as in PHP.

http://booking.com is using a servies like this to display hotels, airports, etc near by a hotel.

I have been using this service for one of my projects.

geonames provides a database dump and they update this periodically i hope.

You can find more information in their site.

http://www.geonames.org/export/web-services.html

http://forum.geonames.org/gforum/posts/list/499.page

樱娆 2024-10-14 08:21:41
<?php
require_once('config.php'); 

$lat = "4.2709781";
$lng = "-120.2881378";
$radius = "10";

$req = $bdd ->prepare("SELECT id, name, lat, lng, ( 6371 * acos( cos( radians('$lat') ) * cos( radians( lat) ) * cos( radians( lng) - radians('$lng') ) + sin( radians('$lat') ) * sin( radians( lat) ) ) ) AS distance FROM markertable HAVING distance < '$radius' ORDER BY distance LIMIT 0 , 40");

$req->execute();

$datas = array();
$i=0;

while($tmpData = $req->fetch()) {
    $datas[$i]['id'] = utf8_encode($tmpData['id']);
    $datas[$i]['name'] = utf8_encode($tmpData['name']);
    $datas[$i]['lat'] = utf8_encode($tmpData['lat']);
    $datas[$i]['lng'] = utf8_encode($tmpData['lng']);
    $datas[$i]['distance'] = utf8_encode($tmpData['distance']);
    $i++;
}

$jsonOutPut = json_encode(array('markers'=>$datas));
echo $jsonOutPut;
$bdd = null;    

?>

<?php
require_once('config.php'); 

$lat = "4.2709781";
$lng = "-120.2881378";
$radius = "10";

$req = $bdd ->prepare("SELECT id, name, lat, lng, ( 6371 * acos( cos( radians('$lat') ) * cos( radians( lat) ) * cos( radians( lng) - radians('$lng') ) + sin( radians('$lat') ) * sin( radians( lat) ) ) ) AS distance FROM markertable HAVING distance < '$radius' ORDER BY distance LIMIT 0 , 40");

$req->execute();

$datas = array();
$i=0;

while($tmpData = $req->fetch()) {
    $datas[$i]['id'] = utf8_encode($tmpData['id']);
    $datas[$i]['name'] = utf8_encode($tmpData['name']);
    $datas[$i]['lat'] = utf8_encode($tmpData['lat']);
    $datas[$i]['lng'] = utf8_encode($tmpData['lng']);
    $datas[$i]['distance'] = utf8_encode($tmpData['distance']);
    $i++;
}

$jsonOutPut = json_encode(array('markers'=>$datas));
echo $jsonOutPut;
$bdd = null;    

?>

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