[英]rounding the latitude and longitude to find the closest one in javascript
[英]Select records with closest Longitude and Latitude
我在数据库中存储了经度和纬度
表名称:km_stores字段:store_id,store_long,store_lat,store_name
如何使用经度和纬度拉最近的商店?
我在用
<script>
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
document.getElementById("km_long").value = position.coords.latitude;
document.getElementById("km_lat").value = position.coords.longitude;
}
</script>
<strong>Long:</strong> <input type="text" id="km_long" /> <strong>Latitude:</strong> <input type="text" id="km_lat" />
[php]
global $wpdb;
$results = $wpdb->get_results('select * from km_stores', OBJECT );
foreach ($results as $store) {
echo $store->store_name;
}
[/php]
[php][/php]
标签,因为我在WordPress Pages Plugin中使用PHP。
使用毕达哥拉斯使它们与您的坐标保持距离。
如果坐标是5、15(出于参数考虑),并且您喜欢的第一个存储是10、20。
$distance = sqrt((5 - 10) * (5 - 10) + (15 - 20) * (15 - 20))
该公式可能会给您一个负数,但是如果您忽略-,则会得到距离。
抱歉,这个答案有点粗糙。
现在,您的select语句将提取所有点,因此您希望将其限制为仅提取附近的点。 最简单的方法是在数据库表中添加lat和lng值的搜索条件。
$query = 'SELECT * FROM km_stores WHERE lat BETWEEN ' . $lat - x . ' AND ' . $lat + x;
$query = $query . ' AND lng BETWEEN ' . $lng - x . ' AND ' . $lng + x;
$results = $wpdb->get_results($query, OBJECT );
假设您已定义了$ lat和$ lng值,它们代表要搜索的点(看起来像您将它们称为km_lat和km_lng)。 然后,您将x替换为您希望点在其中的经/纬度距离。 尝试使用值来确定哪个数字适用于您的应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.