Skip to content

Commit 282787e

Browse files
committed
WIP: Update javascript
- Replace javascript for loops with jinja. I did this in order to create "dynamic" js variables to have an "img" icon. - This is not clever though. I will revert the js loop and then use jinja2 to create the img element when needed
1 parent d815bf0 commit 282787e

File tree

3 files changed

+38
-24
lines changed

3 files changed

+38
-24
lines changed

examples/simple.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
GoogleMaps(app)
88

99
red = Pin(border_color="", glyph_color="",
10-
background="")
10+
background="",
11+
glyph="https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png")
1112
blue = Pin(border_color="blue", glyph_color="",
12-
background="black")
13+
background="black",
14+
glyph="https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png")
1315

1416

1517
@app.route("/")
@@ -20,7 +22,7 @@ def map_created_in_view():
2022
lat=37.4419,
2123
lng=-122.1419,
2224
markers={
23-
red: [(37.4419, -122.1419), (37.4500, -122.1350)],
25+
red: [(37.4419, -122.1419),],
2426
blue: [(37.4300, -122.1400, "Hello World")],
2527
},
2628
style="height:400px;width:600px;margin:0;",

flask_googlemaps/pin.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from dataclasses import dataclass
1+
import uuid
2+
from dataclasses import dataclass, field
23
from typing import Optional
34

45

@@ -8,4 +9,5 @@ class Pin:
89
glyph_color: str
910
background: str
1011
glyph: Optional[str] = None
12+
uuid: Optional[str] = field(default_factory=lambda: f"_{str(uuid.uuid1())[0:8]}")
1113
scale: float = 1.0

flask_googlemaps/templates/googlemaps/gmapjs.html

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -83,41 +83,51 @@
8383

8484
// add gmap markers
8585
const { AdvancedMarkerElement, PinElement } = await google.maps.importLibrary("marker");
86-
87-
for (const marker of {{gmap.markers|tojson|safe}}) {
88-
tempMarker = new AdvancedMarkerElement({
89-
position: new google.maps.LatLng(marker.lat, marker.lng),
86+
{% for marker in gmap.markers %}
87+
{# {% if "http" in marker.pin.glyph %}#}
88+
const {{ marker['pin'].uuid }} = document.createElement("img");
89+
{{ marker['pin'].uuid }}.src = "{{marker['pin'].glyph}}";
90+
{# {% else %}#}
91+
{# const {{ marker.pin.uuid }} = new PinElement({#}
92+
{# glyphColor: marker.pin.glyph_color,#}
93+
{# background: marker.pin.background,#}
94+
{# borderColor: marker.pin.border_color,#}
95+
{# glyph: marker.pin.glyph,#}
96+
{# }).element#}
97+
{# {% endif %}#}
98+
99+
tempMarker = new AdvancedMarkerElement({
100+
position: new google.maps.LatLng({{marker['lat']}}, {{marker['lng']}}),
90101
map: {{gmap.varname}},
91-
title: marker.title ? marker.title : null,
92-
content: new PinElement({
93-
glyphColor: marker.pin.glyph_color,
94-
background: marker.pin.background,
95-
borderColor: marker.pin.border_color,
96-
glyph: marker.pin.glyph,
97-
}).element,
102+
title: {{marker['title'] if marker['title'] is defined else 'null'}},
103+
content: {{ marker['pin'].uuid }}
98104
});
99105

100106

101-
if(marker.infobox)
102-
{
107+
{% if marker['infobox'] is defined %}
108+
103109
google.maps.event.addListener(
104110
tempMarker,
105111
'click',
106-
getInfoCallback({{gmap.varname}}, marker.infobox)
112+
getInfoCallback({{gmap.varname}}, "{{marker['infobox']}}")
107113
);
108-
}
109114

110-
if("{{gmap.report_markerClickpos}}" === "True" )
111-
{
115+
{% endif %}
116+
117+
{% if gmap.report_markerClickpos %}
112118
google.maps.event.addListener(
113119
tempMarker,
114120
'click',
115-
function (event) { markerClickposCallback('{{gmap.markerClickpos_uri}}', event.latLng, marker.title, marker.label)}
121+
function (event) { markerClickposCallback('{{gmap.markerClickpos_uri}}', event.latLng, marker['title'], marker['label'])}
116122
);
117-
}
123+
{% endif %}
118124
{{gmap.varname}}_markers.push(tempMarker);
119125

120-
}
126+
{% endfor %}
127+
128+
{#for (const marker of {{gmap.markers|tojson|safe}}) {#}
129+
{##}
130+
{#}#}
121131

122132

123133
{% if gmap.report_clickpos %}

0 commit comments

Comments
 (0)