0
我已经实现了使用postgis和geodjango从给定坐标显示附近餐厅的功能。但是我需要根据距离用户或给定坐标的距离以km或m为单位查找距离。我知道SO中有关距离的问题,但这个问题有点不同。我正在显示餐厅列表(列表视图),而不是餐厅的详细信息,因此我将从ID中获取特定的餐馆位置。所以我需要一个想法,我现在应该如何显示餐厅列表视图中每个餐厅的距离。显示每个餐厅的公里或米的距离
我的想法是我应该通过lat和LNG(即我从URL中传递),作为做
from django.contrib.gis.geos import GEOSGeometry
pnt = GEOSGeometry('SRID=4326;POINT(40.396764 -3.68042)')
pnt2 = GEOSGeometry('SRID=4326;POINT(48.835797 2.329102 )')
pnt.distance(pnt2)*100
这里计算的距离范围内,并使用模板过滤器是详细
代码def nearby_restaurant_finder(request, current_lat, current_long):
from django.contrib.gis.geos import Point
from django.contrib.gis.measure import D
user_location = Point(float(current_long), float(current_lat))
distance_from_point = {'km': 500}
restaurants = Restaurant.gis.filter(
location__distance_lte=(user_location, D(**distance_from_point)))
restaurants = restaurants.distance(user_location).order_by('distance')
context = {
'restaurants': restaurants
}
return render(request, 'restaurant/nearby_restaurant.html', context)
url(r'^nearby_restaurant/(?P<current_lat>-?\d*.\d*)/(?P<current_long>-?\d*.\d*)/$',
views.nearby_restaurant_finder, name="nearby-restaurant"),
{% block page %}
{% for restaurant in restaurants %}
<h1>Nearby Restaurants are:</h1>
<h3>{{ restaurant.name }}</h3>
{% empty %}
<h3>No Match Found</h3>
{% endfor %}
{% endblock %}
请分享你的想法如何,我应该这样做
非常感谢您的解决方案。我做了以下显示距离,但不知道这是否工作。我怎么知道它? https://gist.github.com/SanskarSans/fe05e1d28552b9f5cc077e0ffa23a2f4 – Serenity
将它与您以前的代码的响应进行比较。当你点击网址时会发生什么? – zsoobhan
解决方案没有问题。我只是要求提示以确定计算的距离是否正确:)。你也有关于距离计算代码的建议吗? – Serenity