Skip to content

Commit a517fd7

Browse files
authored
Merge pull request #64 from n0d/master
Add option to center map based on user's location (Issue #52).
2 parents 17768b2 + e98657a commit a517fd7

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,11 @@ Also controls True or False:
142142
- scale_control
143143
- scale_control
144144
- streetview_control
145-
- rorate_control
145+
- rotate_control
146146
- fullscreen_control
147147
- scroll_wheel
148148
- collapsible (map collapses by click on **varname**_collapse button)
149+
- center on user location (using HTML5 Geolocation)
149150

150151
#### 2. Template
151152

flask_googlemaps/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def __init__(self,
3939
cluster_imagepath=DEFAULT_CLUSTER_IMAGE_PATH,
4040
cluster_gridsize=60,
4141
fit_markers_to_bounds=False,
42+
center_on_user_location=False,
4243
**kwargs):
4344
"""Builds the Map properties"""
4445
self.cls = cls
@@ -68,6 +69,7 @@ def __init__(self,
6869
self.scroll_wheel = scroll_wheel
6970
self.fullscreen_control = fullscreen_control
7071
self.collapsible = collapsible
72+
self.center_on_user_location = center_on_user_location
7173

7274
self.cluster = cluster
7375
self.cluster_imagepath = cluster_imagepath
@@ -726,6 +728,7 @@ def as_json(self):
726728
'cluster_imagepath': self.cluster_imagepath,
727729
'cluster_gridsize': self.cluster_gridsize,
728730
'collapsible': self.collapsible,
731+
'center_on_user_location': self.center_on_user_location,
729732
'js': dumps(self.js),
730733
'html': dumps(self.html),
731734
'fit_markers_to_bounds': self.fit_markers_to_bounds,

flask_googlemaps/templates/googlemaps/gmapjs.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@
4242
fullscreenControl: {% if gmap.fullscreen_control %}true{% else %}false{% endif %}
4343
});
4444

45+
//center map location on user location
46+
{% if gmap.center_on_user_location %}
47+
if (navigator.geolocation) {
48+
navigator.geolocation.getCurrentPosition(function (position) {
49+
initialLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
50+
map.setCenter(initialLocation);
51+
});
52+
}
53+
{% endif %}
54+
4555
// add gmap markers
4656
var raw_markers = {{gmap.markers|tojson|safe}};
4757
for(i=0; i<{{gmap.markers|length}};i++) {

0 commit comments

Comments
 (0)