Skip to content

Commit b5d8104

Browse files
committed
Merge branch 'kjellreell-issue60-latlon-from-click'
2 parents f28ffaf + 91c8171 commit b5d8104

File tree

5 files changed

+73
-5
lines changed

5 files changed

+73
-5
lines changed

examples/example.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
# coding: utf-8
22

3-
from flask import Flask, render_template
3+
from flask import Flask, render_template, request
44
from flask_googlemaps import GoogleMaps
55
from flask_googlemaps import Map, icons
66

77
app = Flask(__name__, template_folder="templates")
88

99
# you can set key as config
10-
app.config['GOOGLEMAPS_KEY'] = "AIzaSyAZzeHhs-8JZ7i18MjFuM35dJHq70n3Hx4"
10+
#app.config['GOOGLEMAPS_KEY'] = "AIzaSyDP0GX-Wsui9TSDxtFNj2XuKrh7JBTPCnU"
1111

1212
# you can also pass key here
13-
GoogleMaps(app, key="AIzaSyAZzeHhs-8JZ7i18MjFuM35dJHq70n3Hx4")
13+
GoogleMaps(
14+
app,
15+
#key="AIzaSyDP0GX-Wsui9TSDxtFNj2XuKrh7JBTPCnU"
16+
)
17+
18+
# NOTE: this example is using a form to get the apikey
1419

1520

1621
@app.route("/")
@@ -307,6 +312,17 @@ def mapview():
307312
}]
308313
)
309314

315+
clickmap = Map(
316+
identifier="clickmap",
317+
varname="clickmap",
318+
lat=37.4419,
319+
lng=-122.1419,
320+
report_clickpos=True,
321+
clickpos_uri="/clickpost/"
322+
)
323+
324+
325+
310326
return render_template(
311327
'example.html',
312328
mymap=mymap,
@@ -320,7 +336,9 @@ def mapview():
320336
movingmap=movingmap,
321337
movingmarkers=movingmarkers,
322338
collapsible=collapsible,
323-
infoboxmap=infoboxmap
339+
infoboxmap=infoboxmap,
340+
clickmap=clickmap,
341+
GOOGLEMAPS_KEY=request.args.get('apikey')
324342
)
325343

326344

@@ -368,8 +386,20 @@ def fullmap():
368386
# maptype = "TERRAIN",
369387
# zoom="5"
370388
)
371-
return render_template('example_fullmap.html', fullmap=fullmap)
389+
return render_template(
390+
'example_fullmap.html',
391+
fullmap=fullmap,
392+
GOOGLEMAPS_KEY=request.args.get('apikey')
393+
)
372394

395+
@app.route('/clickpost/', methods=['POST'])
396+
def clickpost():
397+
# Now lat and lon can be accessed as:
398+
lat = request.form['lat']
399+
lng = request.form['lng']
400+
print(lat)
401+
print(lng)
402+
return "ok"
373403

374404
if __name__ == "__main__":
375405
app.run(debug=True, use_reloader=True)

examples/templates/example.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,19 @@
1616
{{pgonmap.js}}
1717
{{collapsible.js}}
1818
{{infoboxmap.js}}
19+
{{clickmap.js}}
20+
1921
</head>
2022
<body>
2123
<h1>Flask Google Maps Example</h1>
2224

25+
Provide your Google Maps API Key to test it.
26+
Get you APIKEY in <a href="https://cloud.google.com/maps-platform/?apis=maps">https://cloud.google.com/maps-platform/?apis=maps</a>
27+
<form action="." method="GET">
28+
<input type="text" name="apikey" id="apikey"/><input type="submit" value="load"/>
29+
</form>
30+
<hr>
31+
2332
<h2> Template function centered, no marker </h2>
2433
{{googlemap("simple-map", 37.4419, -122.1419, varname="simplemap")}}
2534
<code>
@@ -331,5 +340,9 @@ <h2 style="display:inline-block;">Collapsible map example</h2>
331340
<h2>Infoboxes for map shapes</h2>
332341
{{infoboxmap.html}}
333342

343+
<h2>LatLon from click to flask and back:</h2>
344+
345+
{{clickmap.html}}
346+
334347
</body>
335348
</html>

examples/templates/example_fullmap.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
</head>
1010
<body>
1111
<h1>Flask Google Maps Full Map Example</h1>
12+
Provide your Google Maps API Key to test it.
13+
Get you APIKEY in <a href="https://cloud.google.com/maps-platform/?apis=maps">https://cloud.google.com/maps-platform/?apis=maps</a>
14+
<form action="." method="GET">
15+
<input type="text" name="apikey" id="apikey"/><input type="submit" value="load"/>
16+
</form>
17+
<hr>
1218
{{ fullmap.html }}
1319

1420
</body>

flask_googlemaps/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ def __init__(self,
4040
cluster_gridsize=60,
4141
fit_markers_to_bounds=False,
4242
center_on_user_location=False,
43+
report_clickpos=False,
44+
clickpos_uri="",
4345
**kwargs):
4446
"""Builds the Map properties"""
4547
self.cls = cls
@@ -70,6 +72,8 @@ def __init__(self,
7072
self.fullscreen_control = fullscreen_control
7173
self.collapsible = collapsible
7274
self.center_on_user_location = center_on_user_location
75+
self.report_clickpos = report_clickpos
76+
self.clickpos_uri = clickpos_uri
7377

7478
self.cluster = cluster
7579
self.cluster_imagepath = cluster_imagepath

flask_googlemaps/templates/googlemaps/gmapjs.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@
7272
}
7373
}
7474

75+
{% if gmap.report_clickpos %}
76+
google.maps.event.addListener(
77+
{{gmap.varname}},
78+
'click',
79+
function(event) { clickposCallback('{{gmap.clickpos_uri}}', event.latLng) }
80+
);
81+
{% endif %}
82+
7583
{% if gmap.fit_markers_to_bounds %}
7684
// fit all markers in bounds
7785
var {{gmap.varname}}_bounds = new google.maps.LatLngBounds();
@@ -198,6 +206,13 @@
198206
infowindow.open(map, this);
199207
};
200208
}
209+
210+
function clickposCallback(uri, latLng) {
211+
xhttp = new XMLHttpRequest();
212+
xhttp.open("POST", uri);
213+
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
214+
xhttp.send("lat=" + latLng.lat() + "&lng=" + latLng.lng());
215+
}
201216

202217
{% if gmap.collapsible %}
203218
function init_{{gmap.identifier}}_button() {

0 commit comments

Comments
 (0)