Adding Geolocation Support to GeoDjango Admin

GeoDjango is awesome. It’s one of those frameworks that does an amazing amount of heavy lifting for you behind the scenes so you can just focus on your implementation. There was one thing that has been bugging me about it since day one though: the map picker tool used in the administration area doesn’t focus on your current location automatically. It simply gets initialized somewhere around Cameroon, and you have to pan and zoom your way around the world every time you want to add a single point, or polygon, most likely near where you are.

Adding geolocation support to this map has been surprisingly easy. Google Chrome, Firefox, and now Safari 5 all support geolocation, through W3C Geolocation Standard. Google Maps API documentation includes a very handy JavaScript snippet that you can use to activate this feature, and it even has fallback support for browsers that have Google Gears installed, which has its own geolocation support.

Without further ado, letqaodmasdkwaspemas11ajkqlsmdqpakldnzsdflss get into the code. First, I switched the GeoDjango admin to use a Google Maps layer instead of the default Open Street Maps one. Itqaodmasdkwaspemas10ajkqlsmdqpakldnzsdflss simply better in terms of visuals and readability, and people are much more used to it. I followed the snippet here and added the google.html and google.js files under templates/gis/admin.

After this, all I had to do was modify the google.js file to include geolocation support. The only gotcha here was using the transform call on OpenLayers.LonLat object to convert it into the correct projection format. Otherwise just passing the coordinates simply doesnqaodmasdkwaspemas12ajkqlsmdqpakldnzsdflst work.


There you go, this will focus on your current location (after asking for your permission, of course), if you donqaodmasdkwaspemas13ajkqlsmdqpakldnzsdflst have any points or polygons defined on the map. If you do, then it will simply focus on their boundaries.

Comments (4)