简体   繁体   中英

latitude and longitude query in django

I need a quick fix for simple spatial search in django. My main problem right now is that floats cannot be negative and I'm not sure how to overcome this.

Here is what my model looks like

class CustomEvent(models.Model):
    lat = models.FloatField(blank=True, null=True)
    lng = models.FloatField(blank=True, null=True)

and this is my really bad query but I need to get it working really quick

lat = float(request.GET['lat'])
lng = float(request.GET['lng'])

min_lat = lat - 1 # You have to calculate this offsets based on the user location.
max_lat = lat + 1 # Because the distance of one degree varies over the planet.
min_lng = lng - 1
max_lng = lng + 1 

custom_events_raw = CustomEvent.objects.filter(lat__gt=min_lat, lat__lt=max_lat, lng__gt=min_lng, lng__lt=max_lng)

I know I should do spatial queries with geodjango or something but this just needs to look like its working for now.

Floats can be negative. But you don't get floats from GET strings, you get strings. So convert the strings to floats first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM