Google Maps and Django
There is a great little python wrapper for the google maps API called pymaps – unfortunately every time I’ve had to implement it I’ve found that I’ve had to edit the wrapper to make it work properly (some of the generated JS doesn’t come out correctly – causing a failure in displaying the map with points correctly).
Essentially it’s just a hack, but it is very functional – you can download the modified version here, I claim no responsibility for this script – as all the hard work has already been one by ashwoods, I’ve just tuned it up a little with some very kludgy code (my javascript is terrible).
There is sparse documentation for this project, so I’ve also put a little code sample below to demonstrate how to actually implement this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | def showmap(): from pymaps import Map, PyMap # import the libraries # Create a map - pymaps allows multiple maps in an object tmap = Map() tmap.zoom = 3 # Latitude and lognitude - see the getcords function # to see how we convert from traditional D/M/S to the DD type # used by Googel Maps lat = 0.0 long = 0.0 # These coordinates are for Hong Kong dlat = "22 15 0 N" dlong = "114 10 60 E" dlat = dcode.lat.split(" ") dlong = dcode.long.split(" ") # Convert the coordinates lat = getcords(float(dlat[0]), float(dlat[1]), float(dlat[2]), dlat[3]) long = getcords(float(dlong[0]), float(dlong[1]), float(dlong[2]), dlong[3]) # Inserts html into the hover effect pointhtml = "Hello!" # Add the point to the map point = (lat, long, pointhtml, icon.id) tmap.setpoint(point) tmap.center = (1.757537,144.492188) # Put your own googl ekey here gmap = PyMap(key=GOOGLE_KEY, maplist=[tmap]) gmap.addicon(icon) # pymapjs exports all the javascript required to build the map! mapcode = gmap.pymapjs() # Do what you want with it - pass it to the template or print it! return mapcode |
Here’s a function to convert Degrees Minutes and Seconds into the code required for Google Maps – this is really usefull if you’re qorking with GPS tagged images (EXIF tags), as they are encoded to this standard. Use as you please:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | def getcords(d, m, s, ind): # Calculate the total number of seconds, # 43'41" = (43*60 + 41) = 2621 seconds. sec = float((m * 60) + s) # The fractional part is total number of # seconds divided by 3600. 2621 / 3600 = ~0.728056 frac = float(sec / 3600) # Add fractional degrees to whole degrees # to produce the final result: 87 + 0.728056 = 87.728056 deg = float(d + frac) # If it is a West or S longitude coordinate, negate the result. if ind == 'W': deg = deg * -1 if ind == 'S': deg = deg * -1 return float(deg) |
One imporant thing to remember when using pymaps is to ensure you initialise the html page correctly to display the map, it’s very suimple – some JS calls need to be included in the <body> tag of your page, the template is below:
1 2 3 |
I’ve excluded the <head> tags from this template, it’s quite simple, just include the {{mapcode}} template tag (or whatever you call it) in the <head> element of the template page, et voila – you can now generate your map code!
Useful links:
Enjoy,
M.
- Published:
- 04.12.08 / 12pm
- Category:
- Feature
- Tags:
- Django, exif, google maps api, pymaps
- Post Navigation:
- « Google Bombing = Marketing?
GPS, Python and Your Phone »
Comments are closed
Comments are currently closed on this entry.