Skip to content
This repository was archived by the owner on Oct 22, 2019. It is now read-only.

Commit edfd040

Browse files
committed
Enabled automatic zoom and centering as the default behavior
1 parent 1b935cd commit edfd040

File tree

5 files changed

+92
-12
lines changed

5 files changed

+92
-12
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,15 @@ First, import the simplemap library
4242
import simplemap
4343
```
4444

45-
Next, initialize your map object: `simplemap.Map()`
45+
Next, initialize your map object: `simplemap.Map()`.
46+
47+
**Note:** By default the map will automatically center and zoom based on points given. For a custom center point, make sure to give `center` a lat/lon value.
48+
4649
#####Parameters:
4750

4851
* `title` - The title of the HTML map webpage
49-
* `center` - `list` object containing the latitude and longitude of where the map should be centered
50-
* `zoom` - Zoom level of the map, defaults to 11 - Optional
52+
* `center` - `list` containing the lat and lon of of map center. By default is `None`, which results in auto centering. - Optional
53+
* `zoom` - Zoom level of the map, defaults to 11 - Optional
5154
* `markers` - `list` of markers, where a marker is a `list`: optional hovertext, lat and lon - Optional
5255
* `html_template` - HTML template used to generate a map. Currently the default value of `basic.html` is the only option - Optional
5356
* `config_file` - JSON file containing the google maps `api_key`, defaults to `config.json` - Optional

example.html

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Example Map</title>
5+
<meta name="viewport" content="initial-scale=1.0">
6+
<meta charset="utf-8">
7+
<link rel="stylesheet" href="simplemap/templates/static/css/basic.css">
8+
</head>
9+
<body>
10+
<div id="map"></div>
11+
<script>
12+
13+
function initMap() {
14+
15+
var bounds = new google.maps.LatLngBounds();
16+
var center_lat_lon = null;
17+
18+
var map = new google.maps.Map(document.getElementById('map'), {
19+
zoom: 11,
20+
center: center_lat_lon
21+
});
22+
23+
var markers = [['Example text', 34.4563, -118.1241], ['', 34.6432, -118.1554]];
24+
25+
26+
for( i = 0; i < markers.length; i++ ) {
27+
var position = new google.maps.LatLng(markers[i][1], markers[i][2]);
28+
var title = markers[i][0]
29+
bounds.extend(position);
30+
marker = new google.maps.Marker({
31+
position: position,
32+
map: map,
33+
title:title
34+
35+
});
36+
37+
}
38+
39+
map.fitBounds(bounds);
40+
}
41+
42+
43+
</script>
44+
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDCOgofJoPt_bsoBncCVCzEbOF8HbTH97g&signed_in=true&callback=initMap"
45+
async defer></script>
46+
</body>
47+
</html>

example.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
map_title = 'Example Map'
44
center_point = [34.5124, -118.1111]
5+
#center_point = None
6+
57
gps_markers = [ ['Example text', 34.4563,-118.1241], [34.6432,-118.1554] ]
68

7-
example_map = simplemap.Map(map_title, center_point, markers=gps_markers, config_file='dev_config.json')
9+
example_map = simplemap.Map(map_title, markers=gps_markers)
810
example_map.write('example.html')

simplemap/map.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,44 @@
1515

1616

1717
TEMPLATES_DIR = 'simplemap/templates'
18+
ZOOM_DEFAULT = 11
1819

1920
class Map(object):
2021

21-
def __init__(self, title, center, zoom=11, markers=None, html_template='basic.html', config_file='config.json'):
22+
def __init__(self, title, center=None, zoom=11, markers=None, html_template='basic.html', config_file='config.json'):
2223

2324
self._env = Environment(loader=FileSystemLoader(TEMPLATES_DIR), trim_blocks=True, undefined=SilentUndefined)
2425

2526
self.title = title
2627
self.template = self._env.get_template(html_template)
2728
self.center = center
29+
self.zoom = zoom
2830
self.config = config_file
2931
self.markers = markers
3032

33+
34+
def set_center(self, center_point):
35+
if center_point is None:
36+
self._center = 'null'
37+
else:
38+
self._center = '{{ lat:{0}, lng:{1}}}'.format(center_point[0], center_point[1])
39+
40+
def get_center(self):
41+
return self._center
42+
43+
44+
def set_zoom(self, zoom):
45+
if zoom is None:
46+
if self.center is not None:
47+
#Don't allow zoom to be null if customer center is given
48+
self._zoom = ZOOM_DEFAULT
49+
else:
50+
self._zoom = 'null'
51+
else:
52+
self._zoom = zoom
53+
54+
def get_zoom(self):
55+
return self._zoom
3156

3257
def set_config(self, config_file):
3358

@@ -45,14 +70,13 @@ def set_config(self, config_file):
4570

4671
except Exception:
4772
print "An unknown error occured while attempting to read {0} config file.".format(config_file)
73+
traceback.print_exc()
4874
sys.exit()
4975

50-
5176
def get_config(self):
5277

5378
return self._config
5479

55-
5680
def set_markers(self, markers):
5781
if markers:
5882
for i in markers:
@@ -61,7 +85,6 @@ def set_markers(self, markers):
6185

6286
self._markers = markers
6387

64-
6588
def get_markers(self):
6689

6790
return self._markers
@@ -70,13 +93,15 @@ def get_markers(self):
7093

7194
config = property(get_config, set_config)
7295
markers = property(get_markers, set_markers)
96+
center = property(get_center, set_center)
97+
zoom = property(get_zoom, set_zoom)
7398

7499

75100
def write(self, output_path):
76101

77102
try:
78103

79-
html = self.template.render(map_title = self.title, center=self.center, markers=self._markers, api_key=self.config['api_key'])
104+
html = self.template.render(map_title = self.title, center=self.center,zoom=self.zoom, markers=self.markers, api_key=self.config['api_key'])
80105
with open(output_path, "w") as output:
81106
output.write(html)
82107

@@ -86,7 +111,8 @@ def write(self, output_path):
86111

87112
except Exception:
88113
print "Undefined error occured while writing generating {0}".format(output_path)
89-
sys.exit(0)
114+
traceback.print_exc()
115+
sys.exit()
90116

91117

92118

simplemap/templates/basic.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
function initMap() {
1414

1515
var bounds = new google.maps.LatLngBounds();
16-
var center_lat_lon = {lat: {{center[0]}}, lng: {{ center[1] }} };
16+
var center_lat_lon = {{center|safe}};
1717

1818
var map = new google.maps.Map(document.getElementById('map'), {
19-
zoom: 11,
19+
zoom: {{ zoom|safe }},
2020
center: center_lat_lon
2121
});
2222

@@ -35,6 +35,8 @@
3535
});
3636

3737
}
38+
39+
{{ 'map.fitBounds(bounds);' if center == 'null' else '' }}
3840
}
3941

4042

0 commit comments

Comments
 (0)