在"X"公里(或英里)内查找城市 [英] Finding Cities within 'X' Kilometers (or Miles)

查看:98
本文介绍了在"X"公里(或英里)内查找城市的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能不清楚,也可能不清楚,如果我不在基地或您需要更多信息,请给我评论.也许对于我想要的PHP,已经有了解决方案.

This may or may not be clear, leave me a comment if I am off base, or you need more information. Perhaps there is a solution out there already for what I want in PHP.

我正在寻找一种功能,该功能可以从经度或纬度值中增加或减去一个距离.

I am looking for a function that will add or subtract a distance from a longitude OR latitude value.

原因:我有一个包含所有经度和纬度的数据库,并且想要形成一个查询以提取X公里(或英里)以内的所有城市.我的查询看起来像这样...

Reason: I have a database with all Latitudes and Longitudes in it and want to form a query to extract all cities within X kilometers (or miles). My query would look something like this...

Select * From Cities Where (Longitude > X1 and Longitude < X2) And (Latitude > Y1 and Latitude < Y2)

 Where X1 = Longitude - (distance)
 Where X2 = Longitude + (distance)

 Where Y1 = Latitude - (distance)
 Where Y2 = Latitude + (distance)

我正在使用PHP和MySql数据库进行工作.

I am working in PHP, with a MySql Database.

也欢迎提出任何建议! :)

Open to any suggestions also! :)

推荐答案

这是一个MySQL查询,将完全执行您想要的操作.请记住,像这样的事物通常是近似值,因为地球不是完美的球形,也不考虑山脉,丘陵,山谷等.我们在

This is a MySQL query that will do exactly what you want. Keep in mind things like this are approximations generally, as the earth is not perfectly spherical nor does this take into account mountains, hills, valleys, etc.. We use this code on AcademicHomes.com with PHP and MySQL, it returns records within $radius miles of $latitude, $longitude.

$res = mysql_query("SELECT
    * 
FROM
    your_table
WHERE
    (
        (69.1 * (latitude - " . $latitude . ")) * 
        (69.1 * (latitude - " . $latitude . "))
    ) + ( 
        (69.1 * (longitude - " . $longitude . ") * COS(" . $latitude . " / 57.3)) * 
        (69.1 * (longitude - " . $longitude . ") * COS(" . $latitude . " / 57.3))
    ) < " . pow($radius, 2) . " 
ORDER BY 
    (
        (69.1 * (latitude - " . $latitude . ")) * 
        (69.1 * (latitude - " . $latitude . "))
    ) + ( 
        (69.1 * (longitude - " . $longitude . ") * COS(" . $latitude . " / 57.3)) * 
        (69.1 * (longitude - " . $longitude . ") * COS(" . $latitude . " / 57.3))
    ) ASC");

这篇关于在"X"公里(或英里)内查找城市的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆