Skip to content

Commit feedbac

Browse files
author
martin kjellin
committed
Issue #60: add support for getting clicked latitude and longitude to flask
1 parent f28ffaf commit feedbac

File tree

4 files changed

+46
-3
lines changed

4 files changed

+46
-3
lines changed

examples/example.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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

@@ -306,6 +306,17 @@ def mapview():
306306
'infobox': 'This is a polyline'
307307
}]
308308
)
309+
310+
clickmap = Map(
311+
identifier="clickmap",
312+
varname="clickmap",
313+
lat=37.4419,
314+
lng=-122.1419,
315+
report_clickpos=True,
316+
clickpos_uri="/clickpost/"
317+
)
318+
319+
309320

310321
return render_template(
311322
'example.html',
@@ -320,7 +331,8 @@ def mapview():
320331
movingmap=movingmap,
321332
movingmarkers=movingmarkers,
322333
collapsible=collapsible,
323-
infoboxmap=infoboxmap
334+
infoboxmap=infoboxmap,
335+
clickmap=clickmap
324336
)
325337

326338

@@ -370,6 +382,12 @@ def fullmap():
370382
)
371383
return render_template('example_fullmap.html', fullmap=fullmap)
372384

373-
385+
@app.route('/clickpost/', methods=['POST'])
386+
def clickpost():
387+
# Now lat and lon can be accessed as:
388+
Lat = request.form['lat']
389+
lng = request.form['lng']
390+
return "ok"
391+
374392
if __name__ == "__main__":
375393
app.run(debug=True, use_reloader=True)

examples/templates/example.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
{{pgonmap.js}}
1717
{{collapsible.js}}
1818
{{infoboxmap.js}}
19+
{{clickmap.js}}
20+
1921
</head>
2022
<body>
2123
<h1>Flask Google Maps Example</h1>
@@ -331,5 +333,9 @@ <h2 style="display:inline-block;">Collapsible map example</h2>
331333
<h2>Infoboxes for map shapes</h2>
332334
{{infoboxmap.html}}
333335

336+
<h2>LatLon from click to flask and back:</h2>
337+
338+
{{clickmap.html}}
339+
334340
</body>
335341
</html>

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)