查找给定坐标的一定范围内的项目 [英] Find items within a certain range of given coordinate

查看:121
本文介绍了查找给定坐标的一定范围内的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于django的网络应用程序存储位置。



我有一个Android手机应用程序,它从网络应用程序中拉出位置,并将位置保存到网络应用程序。这些位置返回给我,我加载到我的应用程序中的地图上。



我想发送我当前的坐标,并返回一定范围内的位置列表。例如,我发送我的位置,并收回半径2公里的物品。 (类似于Google Places API的工作原理,只需搜索我的数据)。



最好发送位置坐标,并在我的python django应用程序中执行所有这些逻辑,然后返回正确位置的列表,并只显示我的地图上的地方。



我不知道从哪里开始这样做。
如何根据给定的一组坐标过滤掉我在某个半径(km)内的存储位置?

解决方案

Haversine Equation 是您问题的答案。但是,解密有点困难,所以这里我提供一个简单的解释:



简单地说:



这是一个示例/示例SQL语句,可以找到距离37,-122坐标25英里的最近的20个位置。它根据该行的纬度/经度和目标纬度/经度(下面的等式中由lat / lng给出)计算距离,然后仅要求距离值小于25的行,命令整个查询按距离,并将其限制为20个结果。以公里而不是英里进行搜索,将3959替换为6371.

  SELECT id,(3959 * acos(cos(弧度(37 )* cos(弧度(lat))* cos(弧度(lng) - 弧度(-122))+ sin(弧度(37))* sin(弧度(纬度))))距离标记的距离HAVING distance& 25 ORDER BY distance LIMIT 0,20; 

您可以将sql转换为所需的任何内容。我的意思是原则保持不变。


I have a django based web app which stores locations.

I have an android mobile app which pulls the locations from the web app and saves locations to the web app. The locations returned back to me I load on a mapoverlay in my app.

I would like to send my current coordinates and return a list of locations which are within a certain range. For example I send my location and get back items within a radius of 2km. (Similar to how the Google Places API works, just searching my data).

It is probably best to send the location coordinates and do all this logic in my python django app, Then return a list of correct locations and just display the places on my map.

I don't know where to begin doing this. How can I filter out my stored locations within certain radius(km) based off a given set of coordinates?

解决方案

Haversine Equation is the answer to your question. However it is slightly difficult to decrypt so here I provide you with a simple explanation:

To put it simply:

Here's the sample/example SQL statement that will find the closest 20 locations that are within a radius of 25 miles to the 37, -122 coordinate. It calculates the distance based on the latitude/longitude of that row and the target latitude/longitude (given by lat/lng in the equation below), and then asks for only rows where the distance value is less than 25, orders the whole query by distance, and limits it to 20 results. To search by kilometers instead of miles, replace 3959 with 6371.

SELECT id, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < 25 ORDER BY distance LIMIT 0 , 20;

You can convert the sql to anything you desire. I mean the principle remains the same.

这篇关于查找给定坐标的一定范围内的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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