|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +from __future__ import (absolute_import, division, print_function) |
| 4 | + |
| 5 | +import json |
| 6 | + |
| 7 | +from branca.element import CssLink, Figure, JavascriptLink, MacroElement |
| 8 | + |
| 9 | +from jinja2 import Template |
| 10 | + |
| 11 | + |
| 12 | +class MousePosition(MacroElement): |
| 13 | + """Add a field that shows the coordinates of the mouse position. |
| 14 | +
|
| 15 | + Uses the Leaflet plugin by Ardhi Lukianto under MIT license. |
| 16 | + https://github.com/ardhi/Leaflet.MousePosition |
| 17 | +
|
| 18 | + Parameters |
| 19 | + ---------- |
| 20 | + position : str, default 'bottomright' |
| 21 | + The standard Control position parameter for the widget. |
| 22 | + separator : str, default ' : ' |
| 23 | + Character used to separate latitude and longitude values. |
| 24 | + empty_string : str, default 'Unavailable' |
| 25 | + Initial text to display. |
| 26 | + lng_first : bool, default False |
| 27 | + Whether to put the longitude first or not. |
| 28 | + Set as True to display longitude before latitude. |
| 29 | + num_digits : int, default '5' |
| 30 | + Number of decimal places included in the displayed |
| 31 | + longitude and latitude decimal degree values. |
| 32 | + prefix : str, default '' |
| 33 | + A string to be prepended to the coordinates. |
| 34 | + lat_formatter : str, default None |
| 35 | + Custom Javascript function to format the latitude value. |
| 36 | + lng_formatter : str, default None |
| 37 | + Custom Javascript function to format the longitude value. |
| 38 | +
|
| 39 | + Examples |
| 40 | + -------- |
| 41 | + >>> fmtr = "function(num) {return L.Util.formatNum(num, 3) + ' º ';};" |
| 42 | + >>> MousePosition(position='topright', separator=' | ', prefix="Mouse:", |
| 43 | + ... lat_formatter=fmtr, lng_formatter=fmtr) |
| 44 | +
|
| 45 | + """ |
| 46 | + _template = Template(""" |
| 47 | + {% macro script(this, kwargs) %} |
| 48 | +
|
| 49 | + var {{ this.get_name() }} = new L.Control.MousePosition( |
| 50 | + {{ this.options }}); |
| 51 | + {{ this.get_name() }}.options["latFormatter"] = {{ this.lat_formatter }}; |
| 52 | + {{ this.get_name() }}.options["lngFormatter"] = {{ this.lng_formatter }}; |
| 53 | + {{ this._parent.get_name() }}.addControl({{ this.get_name() }}); |
| 54 | +
|
| 55 | + {% endmacro %} |
| 56 | + """) # noqa |
| 57 | + |
| 58 | + def __init__(self, position='bottomright', separator=' : ', |
| 59 | + empty_string='Unavailable', lng_first=False, num_digits=5, |
| 60 | + prefix='', lat_formatter=None, lng_formatter=None): |
| 61 | + |
| 62 | + super(MousePosition, self).__init__() |
| 63 | + self._name = 'MousePosition' |
| 64 | + |
| 65 | + options = { |
| 66 | + 'position': position, |
| 67 | + 'separator': separator, |
| 68 | + 'emptyString': empty_string, |
| 69 | + 'lngFirst': lng_first, |
| 70 | + 'numDigits': num_digits, |
| 71 | + 'prefix': prefix, |
| 72 | + } |
| 73 | + self.options = json.dumps(options, sort_keys=True, indent=2) |
| 74 | + self.lat_formatter = lat_formatter or 'undefined' |
| 75 | + self.lng_formatter = lng_formatter or 'undefined' |
| 76 | + |
| 77 | + def render(self, **kwargs): |
| 78 | + super(MousePosition, self).render() |
| 79 | + |
| 80 | + figure = self.get_root() |
| 81 | + assert isinstance(figure, Figure), ('You cannot render this Element ' |
| 82 | + 'if it is not in a Figure.') |
| 83 | + |
| 84 | + figure.header.add_child(JavascriptLink('https://cdn.rawgit.com/ardhi/Leaflet.MousePosition/c32f1c84/src/L.Control.MousePosition.js')) # noqa |
| 85 | + |
| 86 | + figure.header.add_child(CssLink('https://cdn.rawgit.com/ardhi/Leaflet.MousePosition/c32f1c84/src/L.Control.MousePosition.css')) # noqa |
0 commit comments