yandex maps templatetag

 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
##https://github.com/coolchevy/django-yandexmaps

##yandexmaps/templatetags/yandexmaps_tags.py
from django.template import Library, TemplateSyntaxError, Node, Variable
from django.template.loader import get_template
from django.template import Context
from django.contrib.sites.models import Site
from yandexmaps.settings import DEFAULT_WIDTH,DEFAULT_HEIGHT,YANDEXMAPS_API_KEY

register = Library()

class YandexMapByAddressNode(Node):
    def __init__(self, address, title, wh):
        self.address = Variable(address)
        self.title = Variable(title)
        self.wh = wh

    def render(self, context):
        if not YANDEXMAPS_API_KEY:
            raise TemplateSyntaxError('YANDEXMAPS_API_KEY is undefined in settings.py')
        address = self.address.resolve(context)
        title = self.title.resolve(context)
        ctx = {
                'map_width':DEFAULT_WIDTH,
                'map_height':DEFAULT_HEIGHT,
                "title":title,
                "address":address,
                "API_KEY":YANDEXMAPS_API_KEY,
                "copyright":Site.objects.get_current().domain,
                }
        if self.wh:
            ctx.update({
                'map_width':self.wh[0],
                'map_height':self.wh[1],
                })
        t = get_template("yandexmaps/map_by_address.html")
        return t.render(Context(ctx))

@register.tag
def yandex_map_by_address(parser, token):
    """
    {% yandex_map_by_address address infobox width,height %}
    """
    bits = token.split_contents()
    if len(bits)<3:
        raise TemplateSyntaxError('%s tag requires more arguments' % bits[0])
    if len(bits) == 4:
        wh = bits[3].split(",")
        if len(wh)<2:
            raise TemplateSyntaxError('%s tag has invalid wight,height argument' % bits[0])
    else:
        wh = None
    return YandexMapByAddressNode(bits[1],bits[2],wh)



##yandexmaps/templates/yandexmaps/map_by_address.html
<script src="http://api-maps.yandex.ru/1.1/index.xml?key={{API_KEY}}" type="text/javascript"></script>
<script type="text/javascript">
var map, geoResult;
YMaps.jQuery(function () {
    map = new YMaps.Map(YMaps.jQuery("#YMapsID")[0]);
    var geocoder = new YMaps.Geocoder('{{address}}', {results: 1, boundedBy: map.getBounds()});
    YMaps.Events.observe(geocoder, geocoder.Events.Load, function () {
        if (this.length()) {
            geoResult = this.get(0);
            map.setBounds(geoResult.getBounds());
    var placemark = new YMaps.Placemark(geoResult.getGeoPoint());
    placemark.name = "{{title}}";
    placemark.description = "{{address}}";
    map.addOverlay(placemark);
    placemark.openBalloon();
        }
    });
    map.addControl(new YMaps.TypeControl());
    map.addControl(new YMaps.Zoom());
    map.addCopyright('{{copyright}}');
});
</script>
<div id="YMapsID" style="width:{{map_width}}px;height:{{map_height}}px;"></div>

More like this

  1. Dynamic Template Loader by jgeewax 5 years, 9 months ago
  2. Uk postcode googlemap templetag by _ben_ 5 years, 1 month ago
  3. Complex Formsets, Redux by smagala 4 years, 1 month ago
  4. ResizeImageField by wim 3 years, 6 months ago
  5. Twin column model admin index by richardbolt 6 years ago

Comments

(Forgotten your password?)