How do I limit results returned with Geolocation?

I have a model which stores a users location:

[
{
    "url": "http://192.168.0.22:8000/status/1/",
    "id": 1,
    "owner": 1,
    "test_info": "",
    "created_at": "2015-05-02T07:09:16.535689Z",
    "updated_at": "2015-05-02T07:09:16.535746Z",
    "geolocation": null,
    "jukebox_mode_enabled": false
},
{
    "url": "http://192.168.0.22:8000/status/2/",
    "id": 2,
    "owner": 2,
    "test_info": "",
    "created_at": "2015-05-02T07:09:24.206959Z",
    "updated_at": "2015-05-02T07:09:24.207042Z",
    "geolocation": null,
    "jukebox_mode_enabled": false
},

I am trying to achieve a system that allows users to query and see who else is nearby but for security reasons I would like to limit the results to users with say 1KM.

What is the best way to achieve this?

PS - The "status" is tied to the normal user model in the django model using a oneToOneField.


What you're looking for is the ability to query/filter by geolocation. Take a look at GeoDjango.

Once you can run the model filter() with a geographical range, then it's just a matter of applying that to your APIView with django-rest-framework.


Two things, you should be using Django's GIS (GeoDjango) features and the GIS plugin for Django REST framework.

GeoDjango will natively work with your database (probably PostGIS) to accurately store and represent the geospatial data. This means you won't have to worry about normalizing locations as they are given to you, and you won't have to manually handle filtering - like finding locations in a radius.

The GIS plugin provides a DistanceToPoint filter that sounds like exactly what you are looking for. You can pass the number of meters in along with the point to use as the center, and it will remove any results that fall outside of that range. This will allow you to use Django REST framework's built-in views and serializers without having to process the querysets and apply the filtering on your own.

链接地址: http://www.djcxy.com/p/26042.html

上一篇: 在Internet Explorer 11中重定向到PDF会导致多个请求

下一篇: 如何限制使用地理位置返回的结果?