Skip to content

Commit d23a0c2

Browse files
Add a simple StreamBlock for setting up a map
1 parent e6fac06 commit d23a0c2

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

wagtailgmaps/blocks.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from wagtail import blocks
2+
from django.conf import settings
3+
4+
from wagtailgmaps.widgets import MapInput
5+
6+
7+
class MapBlock(blocks.CharBlock):
8+
class Meta:
9+
icon = "site"
10+
template = "wagtailgmaps/blocks/map_block.html"
11+
12+
def __init__(
13+
self, *args, latlngMode=False, default_centre=None, zoom=None, **kwargs
14+
):
15+
kwargs["form_classname"] = (
16+
kwargs.get("form_classname", "") + " gmap gmap--latlng"
17+
)
18+
super().__init__(*args, **kwargs)
19+
self.field.widget = MapInput(
20+
default_centre=default_centre or settings.WAGTAIL_ADDRESS_MAP_CENTER,
21+
zoom=zoom or settings.WAGTAIL_ADDRESS_MAP_ZOOM,
22+
latlngMode=latlngMode, # 'latlngMode' will always store lat & lng instead of address
23+
)
24+
25+
def get_context(self, value, parent_context=None):
26+
context = super().get_context(value, parent_context=None)
27+
context["api_key"] = getattr(settings, "GOOGLE_MAPS_KEY", "")
28+
return context

0 commit comments

Comments
 (0)