简体   繁体   English

使用php在距当前位置一定距离内选择餐厅表条目

[英]select restaurant table entries within a certain distance from current location using php

i have a database table with a list of restaurants including their names, latitudes, and longitudes. 我有一个数据库表,其中包含餐厅的列表,包括餐厅的名称,纬度和经度。 i would like to select all the restaurants that are within a certain distance from my current location. 我想选择距离我当前位置一定距离内的所有餐厅。 the current location is determined in the php file (right now i'm just using a static lat & lng). 当前位置在php文件中确定(现在我只是使用静态lat&lng)。 i found code for calculating the distance: 我找到了计算距离的代码:

function findDist($lat, $lng){
    $currLat = 33.777563;
    $currLng = -84.389959;

    $currLat = deg2rad ($currLat);
    $sincurrLat = sin ($currLat);
    $lat = deg2rad ($lat);
    $currLng = deg2rad ($currLng);
    $lng = deg2rad ($lng);

    return round((7926 - 26 * $sincurrLat) * asin (min (1, 0.707106781186548 * sqrt ((1 - (sin ($lat) * $sincurrLat) - cos ($currLat) * cos ($lat) * cos ($lng - $currLng))))),4);
}  

but how do i incorporate that into my select query? 但是如何将其合并到我的选择查询中? i tried this: 我试过这个:

$query = "SELECT *
FROM
    eateries E
WHERE
    EXISTS
    (
        SELECT *
        FROM
            eateries_hours EH, eateries_type ET
        WHERE
            EH.eateries_id = E.id AND ET.eateries_id = E.id
            AND findDist(E.lat, E.lng) <= .5
    )";

but of course that doesn't work because it's not recognizing the function. 但这当然不起作用,因为它无法识别该功能。 can i do a separate query just for lats and lngs at first, calculate the distances, and then join that with the above query somehow? 我可以先对纬度和经度进行单独的查询,计算距离,然后以某种方式将其与上述查询结合吗? any ideas? 有任何想法吗?

thanks. 谢谢。

Take a look at a question that I previously answered. 看一下我之前回答的问题 You can create a stored function in MySQL (or other databases) that does this provided that you have the latitudes and longitudes. 您可以在MySQL(或其他数据库)中创建一个存储函数,前提是您具有经度和纬度。

DELIMITER $$

DROP FUNCTION IF EXISTS `FindDist` $$
CREATE FUNCTION `FindDist` (lt1 DOUBLE,lg1 DOUBLE,lt2 DOUBLE,lg2 DOUBLE) RETURNS DOUBLE
DETERMINISTIC
BEGIN
    DECLARE dist,eradius DOUBLE;

    SET eradius=3963.1;
    SET dist=Acos(Cos(lt1) * Cos(lg1) * Cos(lt2) * Cos(lg2) + Cos(lt1) * Sin(lg1) * Cos(lt2) * Sin(lg2) + Sin(lt1) * Sin(lt2)) * eradius;
    RETURN dist;
END $$

DELIMITER ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用mongodb和php查找最近的餐厅位置 - find nearest restaurant location using mongodb and php 使用PHP使用两个日期字段和当前月份选择表条目 - Using PHP to select table entries using two date fields and current month 使用GPS坐标查找距当前位置的距离 - Find distance from current location using gps coordinates 如何使用PHP从MySQL的表格中选择某些字段 - How to select certain fields from table in mySQL using PHP PHP MySQL使用SELECT仅对某些条目获取SUM - PHP MySQL get SUM for only certain entries using SELECT 使用php以人类可读格式从数据库表打印餐厅营业时间 - printing restaurant opening hours from a database table in human readable format using php 使用PHP MySQL计算从数据库中选择的2个位置之间的距离 - calculate distance between 2 location selected from a database using php mysql 使用php从表中选择*,其中2个条目必须匹配 - use php to select * from table where 2 entries have to match 如何使用经度和纬度计算餐厅与该餐厅附近的 10 个司机之间的距离? - How to calculate the distance between a restaurant and 10 drivers who are nearby to that restaurant restaurant using longitude and latitude? 是表中存储的项目半径范围内的当前位置? - is current location within radius of items stored in table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM