On Sep 15, 5:05 pm, chiggsy <[EMAIL PROTECTED]> wrote:
> It makes a fair bit of sense.. I've heard a lot of good things about
> jquery. If you could post the view later I think I'll be off and
> running. Thank you for the help with this. I find the difficulty
> with web coding is beginnings... I think that I'm getting a good idea
> on how to proceed.
>
Here's my code. If anyone else has critique of it, I'd also love to
hear it. I'm pretty new to this myself.
#In views.py
def locations_in(request):
"Provide a JSON-encoded list of the locations within a polygon"
region = util.get_lookup_region(request.GET)
locations = [{'name':l.name, 'latitude':l.location.y,
'longitude':l.location.x, 'id':l.pk}
for l
in Place.objects.filter(location__within=region)]
return HttpResponse(simplejson.dumps(locations))
#In a util module
def get_lookup_region(querydict):
#Collect the provided points out of a querydict
i = 1
points = []
while "lat%s"%i in querydict and "long%s"%i in querydict:
p = "%s %s" % (querydict["long%s"%i], querydict["lat%s"%i])
points.append(p)
i += 1
#If the body of the while was never executed then there were no
points provided
if i == 1:
raise NoPointsProvidedException()
#Close the polygon if necessary
if points[0] != points[-1]:
points.append(points[0])
#Finally return the geometric region object corresponding to the
above points
lookup_region = GEOSGeometry("POLYGON(( " + ", ".join(points) +
" ))")
return lookup_region
#In models.py
class Place(models.Model):
name = models.CharField(max_length=200)
location = models.PointField()
objects = models.GeoManager()
I hope this is useful for you.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---