2017-10-13 122 views
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 %} 

请分享你的想法如何,我应该这样做

回答

1

我觉得你几乎没有;我会使用python来计算距离,然后将它们显示在模板中而不是创建过滤器。

我会先更新词典列表或相似的背景:

def calculate_distance(restaurant_location, current_lat, current_long): 
    # this function should return the distance of the restaurant from the user 
    return distance_calculated 

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') 

    # create a list of dictionaries with results to display 
    ctx_restaurants = [ 
     { 
      'name': restaurant.name, 
      'distance_from_user': calculate_distance(restaurant.location, current_lat, current_long) 
     } 
     for restaurant in restaurants 
    ] 

    # pass results into context 
    context = { 
     'restaurants': ctx_restaurants 
    } 
    return render(request, 'restaurant/nearby_restaurant.html', context) 

然后,我会在某种表呈现此模板

{% block page %} 
    <h1>Nearby Restaurants are:</h1> 
    <table> 
    {% for restaurant in restaurants %} 
     <tr> 
     <td>{{ restaurant.name }}</td> 
     <td>{{ restaurant.distance_from_user}}</td> 
     </tr> 
    {% endfor %} 
    </table> 
{% endblock %} 

使用TDD: 由于calculate_distance()是解耦的,我会通过传递一堆已知距离来测试它。按照the testing docs设置测试

from django.test import TestCase 
from myapp.views import calculate_distance 

class DistanceTests(TestCase): 
    def setUp(self): 
     self.known_cases = [ 
      {'location': XX1, 'lat': XX1, 'long': XX1, 'expected': XX1}, 
      {'location': XX2, 'lat': XX2, 'long': XX2, 'expected': XX2}, 
     ] 

    def test_calculate_distance(self): 
     for case in self.known_cases: 
      self.assertEquals(
       calculate_distance(case['location'], case['lat'], case['long']), 
       case['expected'] 
      ) 
+0

非常感谢您的解决方案。我做了以下显示距离,但不知道这是否工作。我怎么知道它? https://gist.github.com/SanskarSans/fe05e1d28552b9f5cc077e0ffa23a2f4 – Serenity

+0

将它与您以前的代码的响应进行比较。当你点击网址时会发生什么? – zsoobhan

+0

解决方案没有问题。我只是要求提示以确定计算的距离是否正确:)。你也有关于距离计算代码的建议吗? – Serenity