database - 如何使用自己的位置数据库在网络应用程序中进行基于位置的搜索

标签 database google-places-api

我想在我的网站上实现基于位置的搜索(即位置 + 半径)。这些位置将添加到我的数据库中。我想我会使用 Google Places API,但我找不到如何让它与自己的位置数据库一起使用。有可能吗?我会很感激一个例子或一些教程的链接。如果没有,还有其他解决方案可以解决问题吗?请记住,我无法在服务器上安装任何东西,因此无法向数据库添加一些地理扩展。

提前致谢。

最佳答案

您可以使用谷歌 Geocoding查找您所在位置坐标的服务。然后,您可以使用这些坐标在数据库中查找位置。 如果您的服务器上有 PHP/MySQL,下面的代码使用 Haversine formaula使用 PDO .

$stmt = $dbh->prepare("SELECT  name, lat, lng, (6372 * acos( cos( radians(?) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(?) ) + sin( radians(?) ) * sin( radians( lat ) ) ) ) AS distance FROM mytable HAVING distance < 1 ORDER BY distance ASC LIMIT 0 , 20");
    // Assign parameters
    $stmt->bindParam(1,$center_lat);//Coordinates of location
    $stmt->bindParam(2,$center_lng);//Coordinates of location
    $stmt->bindParam(3,$center_lat);

如果使用 kms 则为 6372,英里为 3959

此查询用于此 DEMO

关于database - 如何使用自己的位置数据库在网络应用程序中进行基于位置的搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15320210/

相关文章:

sql - 数据库表中的搜索模式

android - 道路 googleapi : Some places missing and lines passing through edifices

ios - Swift 3 iOS -Google GMSAutoComplete 自升级后出现故障

Android PlacePicker 在启动后 2 秒关闭

ios - GMSAutocompletePrediction prediction.attributedFullText 不在 swift 3.0 中给出 nsmutablestring

database - 面向对象数据库中的序列属性操作?

database - 在哪里放置我需要的 Symfony2 自定义参数?

sql - 相当于 "when deleting the user, delete all his posts"的数据库概念是什么?

android - 无法读取数据库android

google-places-api - 巴士站详情