https://github.com/coolchevy/django-yandexmaps
django-yandexmaps
easy maps integration via yandex maps api
Installation
- Add your YANDEXMAPS_API_KEY to settings.py http://api.yandex.ru/maps/form.xml/
- Add 'yandex_maps' in INSTALLED_APPS
Usage
Template::
{% load yandexmaps_tags %}
{% yandex_map_by_address object.get_address object.title 500,300 %}
Demo
http://colorsound.com.ua/clubs/porter-pub-1/
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
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
Please login first before commenting.