Login

ReST google-map directive.

Author:
mucius
Posted:
May 17, 2013
Language:
Python
Version:
Not specified
Score:
0 (after 0 ratings)

Use this directive to show google-map if you don't want to use 'raw' directive. default location is my favorite place. Of course you can change it :)

 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
from docutils import nodes
from docutils.parsers.rst import directives, Directive


FORM1 = (
    u'<div id="{0}" class="rst-gmap" style="width:{1}; height:{2}">\r\n'
    u'</div>\r\n'
    u'<script type="text/javascript">$(function(){{\r\n'
    u'  show_gmap("{0}", {3}, {4}, {5}, "{6}");\r\n'
    u'}})</script>'
)


class Gmap(Directive):
    """ Google map directive """
    required_arguments = 1
    optional_arguments = 0
    final_argument_whitespace = False
    option_spec = {
        'height': directives.length_or_unitless,
        'width': directives.length_or_percentage_or_unitless,
        'zoom': directives.nonnegative_int,
        'latlng' : directives.unchanged,
        'title': directives.unchanged,
    }

    def run(self):
        map_id = self.arguments[0].strip()
        val = {
            'height': '240px',
            'width' : '320px',
            'zoom' : '15',
            'latlng' : '60.996572,24.462588',
            'title' : 'here!',
        }
        for options in ['height', 'width', 'zoom', 'latlng', 'title']:
            if options in self.options:
                val[options] = self.options[options]
        try:
            latitude, longitude = [x.strip() for x in val['latlng'].split(',')]
        except ValueError:
            raise self.error('Invalid latitude and/or longitude value.')
        try:
            html = FORM1.format(map_id, val['width'], val['height'],
                                latitude, longitude, val['zoom'], val['title'])
        except IOError:
            raise self.error('Could not convert to html. please contact to author.')
        return [nodes.raw('', html, format='html')]


directives.register_directive('gmap', Gmap)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 8 months ago

Comments

Please login first before commenting.