我正在开发一个小应用程序,它使用GeoDjango在附近的场地找到演出。这是我的models.py
:
from django.contrib.gis.db import models
class Venue(models.Model):
"""
Model for a venue
"""
name = models.CharField(max_length=200)
location = models.PointField()
def __str__(self):
return self.name
class Event(models.Model):
"""
Model for an event
"""
name = models.CharField(max_length=200)
datetime = models.DateTimeField()
venue = models.ForeignKey(Venue)
def __str__(self):
return "%s - %s" % (self.name, self.venue.name)
现在,我已经设法让它进行查找,它看起来像我需要将响应序列化到geojson中以便在地图上呈现它。但是,我正在努力完成它。这是我目前的views.py
:
from django.shortcuts import render_to_response
from django.views.generic.edit import FormView
from gigs.forms import LookupForm
from gigs.models import Event
from django.utils import timezone
from django.contrib.gis.geos import Point
from django.contrib.gis.db.models.functions import Distance
from django.template import RequestContext
class LookupView(FormView):
form_class = LookupForm
def get(self, request):
return render_to_response('gigs/lookup.html', RequestContext(request))
def form_valid(self, form):
# Get data
latitude = form.cleaned_data['latitude']
longitude = form.cleaned_data['longitude']
# Get next week's date
next_week = timezone.now() + timezone.timedelta(weeks=1)
# Get Point
location = Point(latitude, longitude, srid=4326)
# Look up events
events = Event.objects.filter(datetime__lte=next_week).annotate(distance=Distance('venue__location', location)).order_by('distance')[0:5]
# Render the template
return render_to_response('gigs/lookupresults.html', {
'events': events
})
如果我在收到事件后插入断点:
ipdb> from gigs.models import Venue
ipdb> from django.core.serializers import serialize
ipdb> venues = Venue.objects.all()
ipdb> serialize('geojson', venues, geometry_field='location', fields=('name',))
'{"type": "FeatureCollection", "crs": {"type": "name", "properties": {"name": "EPSG:4326"}}, "features": [{"type": "Feature", "geometry": {"type": "Point", "coordinates": [77.13845850820013, 88.27032065635657]}, "properties": {"name": "Venue1"}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [-144.48274402224723, -35.87841402981486]}, "properties": {"name": "Venue2"}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [15.249714163005194, -39.942840871151624]}, "properties": {"name": "Venue3"}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [-63.376261279235095, -6.222101297964656]}, "properties": {"name": "Venue4"}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [153.29028611820962, -4.285826286375041]}, "properties": {"name": "Venue5"}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [-146.08227004805758, 42.4843671723977]}, "properties": {"name": "Venue6"}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [149.6004493621263, 34.740389078323844]}, "properties": {"name": "Venue7"}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [-144.38342519084884, -55.55425529324768]}, "properties": {"name": "Venue8"}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [83.32120247931198, 48.78215628903402]}, "properties": {"name": "Venue9"}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [-28.108531225500826, 35.50271671578547]}, "properties": {"name": "Venue10"}}]}'
ipdb> serialize('geojson', events, geometry_field='venue__location', fields=('name',))
'{"type": "FeatureCollection", "crs": {"type": "name", "properties": {"name": "EPSG:4326"}}, "features": [{"type": "Feature", "geometry": null, "properties": {"name": "Event3"}}, {"type": "Feature", "geometry": null, "properties": {"name": "Event9"}}, {"type": "Feature", "geometry": null, "properties": {"name": "Event10"}}, {"type": "Feature", "geometry": null, "properties": {"name": "Event1"}}, {"type": "Feature", "geometry": null, "properties": {"name": "Event7"}}]}'
我可以获取场地列表的位置,但似乎无法从Event
模型获取场地的位置。
知道我哪里出错了?
答案 0 :(得分:1)
希望这不会太晚,但答案是你不需要序列化到GeoJSON来渲染到地图中。你可以使用它,但还有很多其他方法。
例如,你可以制作一个JSON数组,其中每个元素都是一个lat,lng坐标对,你的javascript可以迭代它并添加到地图中。