Map API's and Django, preferences?

43 views
Skip to first unread message

chiggsy

unread,
Sep 12, 2008, 9:02:58 PM9/12/08
to Django users
I'm working on a little idea. I need to display data on a map. Now,
I have been reading over the gis stuff in django, I've set up
PostGIS , and now i'm trying to work out how do display the data.
What are some opinons on Google maps vs yahoo maps, for ease of django
development ? (I am brand new to this, so if i missed a handy map api
pls lmk)

TiNo

unread,
Sep 13, 2008, 10:15:13 AM9/13/08
to django...@googlegroups.com
I wanted to do the same. I decided not to user GIS because of all the new stuff I needed to install. Two Coordinates are good enough for the google maps api. 

I plan on using geopy (http://exogen.case.edu/projects/geopy/) for translating addresses to coordinates.

I also found these articles that should help you in the right direction:

Good luck, and please backpost when you build a cool app!

TiNo

David Christiansen

unread,
Sep 15, 2008, 4:09:16 AM9/15/08
to Django users
I've had good luck with using GeoDjango and a custom Google Maps
interface. The one included with GeoDjango was a bit sparse in
features, but it did work. An easy way to get started might be to
have GeoDjango generate you a map, and then take the generated code
and use it as a starting point and put your own information in it.

What I do is I connect the 'move' event of the Google map to a little
function that makes a GET request to a Django view with the lat/long
of the four corners of the map as parameters. This returns a list of
points inside the polygon formed by the four points, along with
metadata, and I then iterate over that list and create GMarker
instances on the map. It's a little more complicated, as it uses a
MarkerManager to hide points that are off the map and a table of
already-seen points to avoid adding them twice, but that's the general
method. It seems to be working pretty well so far, though I could
decrease server load by only querying the polygons that haven't been
queried before.

Have you written Javascript before?

-David



On Sep 13, 4:15 pm, TiNo <tin...@gmail.com> wrote:
> I wanted to do the same. I decided not to user GIS because of all the new
> stuff I needed to install. Two Coordinates are good enough for the google
> maps api.
> I plan on using geopy (http://exogen.case.edu/projects/geopy/) for
> translating addresses to coordinates.
>
> I also found these articles that should help you in the right direction:http://www.developer.com/java/web/article.php/10935_3528381http://ajaxian.com/archives/geocoding-with-googles-maps-apihttp://www.instantdjango.com/chapter1.html
>
> Good luck, and please backpost when you build a cool app!
>
> TiNo
>

chiggsy

unread,
Sep 15, 2008, 6:46:29 AM9/15/08
to Django users
Yes I've written javascript before. Not for a long, and
coincidentally I'm sure, pleasant time. ;) Thanks for the tip though,
I'll look that over today.
> > I also found these articles that should help you in the right direction:http://www.developer.com/java/web/article.php/10935_3528381http://aja...
>
> > Good luck, and please backpost when you build a cool app!
>
> > TiNo
>
> > On Sat, Sep 13, 2008 at 3:02 AM, chiggsy <lazy...@gmail.com> wrote:
>
> > > I'm working on a little idea.  I need to display data on a map.  Now,
> > > I have been reading over the gis stuff in django, I've set up
> > > PostGIS , and now i'm trying to work out how do display the data.
> > > What are some opinons on Googlemapsvs yahoomaps, for ease of django

David Christiansen

unread,
Sep 15, 2008, 9:52:07 AM9/15/08
to Django users
Here's what my code looks like to get information from the server to
display on the map:

function update_locations () {
bounds = map.getBounds();
sw = bounds.getSouthWest();
ne = bounds.getNorthEast();
$.getJSON('/locations/locations/',
{'lat1':sw.lat(), 'long1':sw.lng(),
'lat2':sw.lat(), 'long2':ne.lng(),
'lat3':ne.lat(), 'long3':ne.lng(),
'lat4':ne.lat(), 'long4':sw.lng()},
function(data, textStatus){
$.each(data, function() {
//Check if currently in set, add if not
if (markerSet["key"+this.id]) {}
else {
latlng = new GLatLng(this.latitude,
this.longitude);
marker = new GMarker(latlng,
{'title':this.name});
marker.bindInfoWindowHtml("<iframe src=\"/
locations/info/"+this.id+"/\"></iframe>");
markerSet["key"+this.id] = marker;
markerManager.addMarker(marker, 10);
}
});
}
);
}

In my onload event handler for the page, I have this:

GEvent.addListener(map, 'moveend', update_locations);

I don't have access to the Django view that responds to the request
right now, but I could post it later if it would be helpful. It
basically just creates a polygon from the lat/long GET parameters and
then does a GeoDjango query for the points inside that polygon. It
then iterates over them and constructs a JSON-encoded list of
JavaScript objects, which it puts into the HTTPResponse.

The code uses JQuery, which I've found really helpful in reducing the
pain of Javascript. That's what all the funny $ variables are. Does
this make sense?

chiggsy

unread,
Sep 15, 2008, 11:05:19 AM9/15/08
to Django users
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.

David Christiansen

unread,
Sep 16, 2008, 3:47:13 AM9/16/08
to Django users
On Sep 15, 5:05 pm, chiggsy <lazy...@gmail.com> 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.
Reply all
Reply to author
Forward
0 new messages