获得用户最近的城市的最佳途径?的Python / Django的 [英] Best way to get user nearest city? Python/Django

查看:212
本文介绍了获得用户最近的城市的最佳途径?的Python / Django的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库中有限数量的城市的网站,并且需要向用户显示他当前所在的城市。

位置由MaxMind API提供,但我想将我的数据库中最近的城市送到用户城市。

例如,如果我在数据库中有这些城市:洛杉矶旧金山纽约市,而我从其他城市如迈阿密访问,我应该看到纽约市被选中,因为它距离最近的地理位置。



什么是最好的方式要做到这一点快速和性能意识?

解决方案

您应该存储每个城市的近似经度和纬度,计算经度和纬度以度为单位,然后使用 Haversine公式查找距离。它在JavaScript 这里中实现。 MaxMind API应该为您提供经纬度。


I have a website with a limited number of cities in the database, and need to show the user the nearest city to his current location.

I can get the location by MaxMind API, but I want to get the nearest city in my database to user city.

For example, if I have these cities in the database: Los Angeles, San Francisco and New York City, and I'm accessing from other city like Miami, I should see NYC selected because it's the nearest geographically.

What's the best way to do this quick and performance aware?

解决方案

You should store the approximate latitude and longitude for each city, calculate the latitude and longitude in degrees for the user, and then find the distance using the Haversine formula. It's implemented in Javascript here. The MaxMind API should give you the latitude and longitude.

这篇关于获得用户最近的城市的最佳途径?的Python / Django的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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