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?
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 models.py, I wrote this geocoder class in my fires models.py:
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 models.py is this:
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 manage.py 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.