Django - 我如何找到两个地点之间的距离? [英] Django - how can I find the distance between two locations?

查看:319
本文介绍了Django - 我如何找到两个地点之间的距离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些用户注册在我的Django应用程序,我想只需要根据他们的邮政编码来确定两个用户之间的距离,然后根据这些排序列表。我会想像这个功能并不是内置到Django中的。我正在看一些选项,偶然发现geodjango,似乎可能对我的需要是过度的。

解决方案

按照tcarobruce的建议,这里是我以上的评论作为答案:



Zip代码数据库项目具有美国邮政编码的纬度和经度的数据库,可以是SQL或CSV。他们还提供以下用于距离计算的代码(由我编辑的slighlty):

 从数学导入sin,cos,弧度,度,acos 

def calc_dist(lat_a,long_a,lat_b,long_b):
lat_a =弧度(lat_a)
lat_b =弧度(lat_b)
long_diff =弧度long_a - long_b)
distance =(sin(lat_a)* sin(lat_b)+
cos(lat_a)* cos(lat_b)* cos(long_diff))
返回度(acos ))* 69.09

请注意,结果以法定里程给出。



编辑:由于John Machin的更正。


I have some users registered in my Django app and I want to simply be able to figure out the distance, geographically, between two users based on their zip code and then sort a list based on that. I would imagine this functionality isn't built into Django. I was looking at some options and stumbled across geodjango which seems like it might be overkill for what my needs are.

解决方案

Following tcarobruce's suggestion, here is my above comment as an answer:

The Zip Code Database Project has a database of the latitudes and longitudes of the US zip codes, either as SQL or as CSV. They also provide the following code for distance calculation (slighlty edited by me):

from math import sin, cos, radians, degrees, acos

def calc_dist(lat_a, long_a, lat_b, long_b):
    lat_a = radians(lat_a)
    lat_b = radians(lat_b)
    long_diff = radians(long_a - long_b)
    distance = (sin(lat_a) * sin(lat_b) +
                cos(lat_a) * cos(lat_b) * cos(long_diff))
    return degrees(acos(distance)) * 69.09

Note that the result is given in statute miles.

Edit: Corrections due to John Machin.

这篇关于Django - 我如何找到两个地点之间的距离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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