Megan Taylor

front-end dev, volunteacher, news & data junkie, bibliophile, Flyers fan, sci-fi geek and kitteh servant

Geocoding addresses in Firetracker for big map of recent fires

We’re gettin’ fancy now! After adding individual maps for each fire, the natural next step was to add a large map of all recent fires. This was a more complicated addition to Firetracker, but check it out! Awesome, right?

Here’s what I did: I started by studying the from django-easy-maps, where the geocoding is done. I made a few attempts to figure out how to just pass the data from easy-maps back to my fires map, but since the model in easy-maps is named Address, and I also had a model in fires called Address, I would have had to rewrite a whole bunch of code…so I decided instead to use the easy-maps geocoder as a model for my own geocoder, so I would have the latitude and longitude variables within fires, and could easily pass that to the Google Maps JavaScript API.

If you’ve already created your Firetracker app and added data, you are going to hate me because this process involves dumping database tables. We are adding fields to a previously existing model, so we have to recreate the tables. I’m sure there’s a way to inject the additional data if you have a lot of data in there already, but since I only had two fires entries, it was easier to just dump.

After studying the easy-maps, I wrote this geocoder class in my fires  

Lines 1 – 6 create the table and its fields. Then there’s some data verification and error handling. Lines 12 – 14 do the actual geocoding. Then more error handling and finally we spit out the latitude and longitude for the address.

I also made some changes to the Address class, adding these two methods:

This adds the geocoded data to the Address class for easier referencing later.

The last change to the is this:

These lines get added to the fire class. The first line (add this with the other fields in the fire class) adds the geocode field to the fire table. The save method makes the geocoder run when you save a new fire entry and saves the geocoded data to the database. Now we just have to use the Google Maps JavaScript API to generate our map!

If you’ve played with the Google Maps API before, this will all look very familiar to you. If it doesn’t, you should go learn about it. You can see in the script above where we’re calling our new geocoded variables in, so the map will automatically create markers for all the most recent fires.

Now, you have to dump all your fire tables. Then run syncdb and restart your server. When you go to the admin to add data, create a new fire entry. When you save it, the address will automagically be geocoded!

I think that’s it! A huge thank you to Ken Schwencke, who helped me figure out how to pass the data around to the Address and Fire classes.

July 14, 2011 | Comments Off on Geocoding addresses in Firetracker for big map of recent fires | Categories: Posts | Permalink

Comments are closed.

%d bloggers like this: