- Author:
- javinievas
- Posted:
- June 27, 2007
- Language:
- HTML/template
- Version:
- Not specified
- Score:
- 13 (after 15 ratings)
Use:
... <head> ... {% gmap-script %} ... </head> ... <body> ... {% gmap name:mimapa width:300 height:300 latitude:x longitude:y zoom:20 view:hybrid %} Message for a marker at that point {% endgmap %} ... </body>
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | from django import template
from django.template import Library
from django.template import RequestContext
from django.template import resolve_variable
GOOGLE_MAPS_API_KEY = {
"site1":"XXXXXAApNC8VraKxjnpTrC4pax3IRQTfsoMw3qz_e-LauZzPBIws5p_nhSKlQQunTrdkST45GbLxANehi-oSQ",
"site2":"FFSDFGSDFGSDFGSDFGrC4pax3IRSA7oaebcwS_GFScocy27dna4926BSle32YWjKmaH2Q-oFoR7xr_lzKAQ",
"site3":"ABQIAAAAXXXXXXXXXnpTrC4pax3IRTjlGRJ-JcA4ENdYSxSTUELqnaldxSXtgc7J9ZfVENFwQjXVhQX0f824A",
}
CURRENT_SITE = "site3"
register = Library()
INCLUDE_TEMPLATE = """
<script src="http://maps.google.com/maps?file=api&v=2&key=%s" type="text/javascript"></script>
""" % (GOOGLE_MAPS_API_KEY [ CURRENT_SITE ] , )
BASIC_TEMPLATE = """
<div id="map_%s" style="width:%spx;height:%spx;"></div>
<script>
function create_map_%s() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map_%s"));
map.enableDoubleClickZoom();
map.enableContinuousZoom();
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
map.setCenter(new GLatLng(%s,%s), %s, map.getMapTypes()[%d]);
var point = map.getCenter();
var m = new GMarker(point);
GEvent.addListener(m, "click", function() {
m.openInfoWindowHtml("%s");
});
map.addOverlay(m);
return map;
}
}
</script>
"""
# {% gmap name:mimapa width:300 height:300 latitude:x longitude:y zoom:20 view:hybrid %} Message for a marker at that point {% endgmap %}
class GMapNode (template.Node):
def __init__(self, params, nodelist):
self.params = params
self.nodelist = nodelist
def render (self, context):
for k,v in self.params.items():
try:
self.params[k] = resolve_variable(v, context)
except:
pass
if k == "view":
if v=="satellite":
v = 1
elif v=="map":
v = 0
else:
v = 2
self.params[k] = v
self.params["message"] = self.nodelist.render(context).replace("\n", "<br />")
return BASIC_TEMPLATE % (self.params['name'], self.params['width'], self.params['height'], self.params['name'], self.params['name'], self.params['latitude'], self.params['longitude'], self.params['zoom'], self.params['view'], self.params['message'])
def do_gmap(parser, token):
items = token.split_contents()
nodelist = parser.parse(('endgmap',))
parser.delete_first_token()
#Default values
parameters={
'name' : "default",
'width' : "300",
'height' : "300",
'latitude' : "33",
'longitude' : "-3",
'zoom' : "15",
'view' : "hybrid", # map, satellite, hybrid
'message' : "No message",
}
for item in items[1:]:
param, value = item.split(":")
param = param.strip()
value = value.strip()
if parameters.has_key(param):
if value[0]=="\"":
value = value[1:-1]
parameters[param] = value
return GMapNode(parameters, nodelist)
class GMapScriptNode (template.Node):
def __init__(self):
pass
def render (self, context):
return INCLUDE_TEMPLATE
def do_gmap_script(parser, token):
try:
tag_name = token.split_contents()
except ValueError:
raise template.TemplateSyntaxError("La etiqueta no requiere argumentos" % token.contents[0])
return GMapScriptNode()
register.tag('gmap', do_gmap)
register.tag('gmap-script', do_gmap_script)
|
More like this
- Bootstrap Accordian by Netplay4 5 years, 11 months ago
- Bootstrap theme for django-endless-pagination? by se210 8 years, 11 months ago
- Bootstrap theme for django-endless-pagination? by se210 8 years, 11 months ago
- Reusable form template with generic view by roldandvg 9 years ago
- Pagination Django with Boostrap by guilegarcia 9 years, 2 months ago
Comments
The function create_map_%s isn't call by my broswer, so no call no map.
I use onload proprety of <body> but it may have a better solution.
#
On row 64 you have a very long string formatting line, where all the formatting elements come from
self.params
. You can simplify the line and improve the readability of theBASIC_TEMPLATE
variable by using parenthesised mapping keys. So instead of writing<div id="map_%s" style="width:%spx;height:%spx;"></div>
you'd use
<div id="map_%(name)s" style="width:%(width)spx;height:%(height)spx;"></div>
And then the actual formatting line would be simply:
return BASIC_TEMPLATE % self.params
#
This belongs in the python section!
#
I use jquery as js lib, so I have added:
jQuery(document).ready(function(){ create_map_%s(); });
rather than onload
#
is this a typo: map.getMapTypes()[%d]
should it be: map.getMapTypes()[%s] ?
or: map.getMapTypes()[%(view)s] if you do as arsatiki suggests (good tip!)
#
Please login first before commenting.