-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGMapMarkers.py
More file actions
297 lines (278 loc) · 14 KB
/
Copy pathGMapMarkers.py
File metadata and controls
297 lines (278 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import json
import requests
from pprint import pprint
from bs4 import BeautifulSoup
import gmplot
import webbrowser
import time
class CreateGoogleMapMarkers(object):
API_KEY = ''
# Set API Key
def __init__(self, center_lat, center_long, zoom, icon, title):
self.center_location = (float(center_lat), float(center_long))
self.zoom = int(zoom)
self.icon = str(icon)
self.title = str(title)
self.key = API_KEY
self.white_lats = []
self.white_longs = []
self.white_titles = []
self.black_lats = []
self.black_longs = []
self.black_titles = []
self.blue_lats = []
self.blue_longs = []
self.blue_titles = []
self.orange_lats = []
self.orange_longs = []
self.orange_titles = []
self.purple_lats = []
self.purple_longs = []
self.purple_titles = []
self.red_lats = []
self.red_longs = []
self.red_titles = []
self.green_lats = []
self.green_longs = []
self.green_titles = []
self.yellow_lats = []
self.yellow_longs = []
self.yellow_titles = []
self.custom_lats = []
self.custom_longs = []
self.custom_titles =[]
@staticmethod
def set_api_key(key):
global API_KEY
API_KEY = key
def map_markers(self, file_name):
# Top of HTML file -> CONSTANT
f = open(file_name + '.html', 'w')
f.write('<!DOCTYPE html>\n')
f.write('<html>\n')
f.write('<head>\n')
f.write('\t<meta http-equiv="content-type" content="text/html; charset=UTF-8" />\n')
f.write('\t<title>Google Maps Markers</title>\n')
f.write('\t<script src="http://maps.google.com/maps/api/js?key=' + API_KEY +
'&sensor=true_or_false&libraries=visualization" type="text/javascript"></script>\n')
f.write('</head>\n')
f.write('<body style="margin:0px; padding:0px;" onload="initialize()">\n')
f.write('\t<div id="map" style="width: 1500px; height: 1000px;"></div>\n')
f.write('\t<script type="text/javascript">\n')
f.write('\t\tfunction initialize() {\n')
# Write current location lat & lons
self.add_center_location(f)
# create locations with different colored markers
self.set_white_markers(f)
self.set_black_markers(f)
self.set_red_markers(f)
self.set_purple_markers(f)
self.set_green_markers(f)
self.set_orange_markers(f)
self.set_blue_markers(f)
self.set_yellow_markers(f)
# Bottom of HTML file -> CONSTANT
f.write('\t\t\tsetMap(locationMarker);\n')
f.write('\t\t}\n')
f.write('\t</script>\n')
f.write('</body>\n')
f.write('</html>\n')
f.close()
# Adds a lat, lng for center of map
def add_center_location(self, f):
f.write('\t\t\tvar mapCenter = new google.maps.LatLng(%f, %f, \'%s\');\n' %
(self.center_location[0], self.center_location[1], self.title))
f.write('\t\t\tvar myOptions = {\n')
f.write('\t\t\t\tzoom: %d,\n' % self.zoom)
f.write('\t\t\t\tcenter: mapCenter,\n')
f.write('\t\t\t\tmapTypeId: google.maps.MapTypeId.ROADMAP\n')
f.write('\t\t\t};\n')
f.write('\t\t\tvar map = new google.maps.Map(document.getElementById(\'map\'), myOptions);\n')
f.write('\n')
f.write('\t\t\tvar locationMarker = new google.maps.Marker({\n')
f.write('\t\t\t\tposition: mapCenter,\n')
f.write('\t\t\t\tanimation: google.maps.Animation.BOUNCE,\n')
f.write('\t\t\t\tmap:map,\n')
if self.icon == 'default':
self.icon = 'pins/pin.png'
f.write('\t\t\t\ticon: \'%s\' \n' % self.icon)
f.write('\t\t\t});\n')
f.write('\t\t\tvar locationWindow = new google.maps.InfoWindow();\n')
f.write('\t\t\tgoogle.maps.event.addListener(locationMarker, \'click\', (function (locationMarker) {\n')
f.write('\t\t\t\treturn function() {\n')
f.write('\t\t\t\t\tlocationWindow.setContent(\'%s\');\n' % self.title)
f.write('\t\t\t\t\tlocationWindow.open(map, locationMarker);\n')
f.write('\t\t\t\t}\n')
f.write('\t\t\t})(locationMarker));\n\n')
# Add marker locations based on marker color
def create_markers(self, latitudes, longitudes, titles, color):
if color == 'white':
self.white_lats.extend(latitudes)
self.white_longs.extend(longitudes)
if titles != "None":
self.white_titles.extend(titles)
elif color == 'red':
self.red_lats.extend(latitudes)
self.red_longs.extend(longitudes)
if titles != "None":
self.red_titles.extend(titles)
elif color == 'orange':
self.orange_lats.extend(latitudes)
self.orange_longs.extend(longitudes)
if titles != "None":
self.orange_titles.extend(titles)
elif color == 'purple':
self.purple_lats.extend(latitudes)
self.purple_longs.extend(longitudes)
if titles != "None":
self.purple_titles.extend(titles)
elif color == 'yellow':
self.yellow_lats.extend(latitudes)
self.yellow_longs.extend(longitudes)
if titles != "None":
self.yellow_titles.extend(titles)
elif color == 'blue':
self.blue_lats.extend(latitudes)
self.blue_longs.extend(longitudes)
if titles != "None":
self.blue_titles.extend(titles)
elif color == 'black':
self.black_lats.extend(latitudes)
self.black_longs.extend(longitudes)
if titles != "None":
self.black_titles.extend(titles)
elif color == 'green':
self.green_lats.extend(latitudes)
self.green_longs.extend(longitudes)
if titles != "None":
self.green_titles.extend(titles)
elif color == 'custom':
self.custom_lats.extend(latitudes)
self.custom_longs.extend(longitudes)
if titles != "None":
self.custom_titles.extend(titles)
def set_white_markers(self, f):
if len(self.white_lats) == len(self.white_longs) and (len(self.white_lats) and len(self.white_longs)) != 0:
if len(self.white_titles) != 0:
self.add_locations_info(f, self.white_lats, self.white_longs,
self.white_titles, 'pins/white.png', 'white')
elif len(self.white_titles) == 0:
self.add_locations_markers(f, self.white_lats, self.white_longs, 'pins/white.png', 'white')
def set_red_markers(self, f):
if len(self.red_lats) == len(self.red_longs) and (len(self.red_lats) and len(self.red_longs)) != 0:
if len(self.red_titles) != 0:
self.add_locations_info(f, self.red_lats, self.red_longs,
self.red_titles, '', 'red')
elif len(self.red_titles) == 0:
self.add_locations_markers(f, self.red_lats, self.red_longs, '', 'red')
def set_orange_markers(self, f):
if len(self.orange_lats) == len(self.orange_longs) and (len(self.orange_lats) and len(self.orange_longs)) != 0:
if len(self.orange_titles) != 0:
self.add_locations_info(f, self.orange_lats, self.orange_longs,
self.orange_titles, 'pins/orange.png', 'orange')
elif len(self.orange_titles) == 0:
self.add_locations_markers(f, self.orange_lats, self.orange_longs, 'pins/orange.png', 'orange')
def set_purple_markers(self, f):
if len(self.purple_lats) == len(self.purple_longs) and (len(self.purple_lats) and len(self.purple_longs)) != 0:
if len(self.purple_titles) != 0:
self.add_locations_info(f, self.purple_lats, self.purple_longs,
self.purple_titles, 'pins/purple.png', 'purple')
elif len(self.purple_titles) == 0:
self.add_locations_markers(f, self.purple_lats, self.purple_longs, 'pins/purple.png', 'purple')
def set_yellow_markers(self, f):
if len(self.yellow_lats) == len(self.yellow_longs) and (len(self.yellow_lats) and len(self.yellow_longs)) != 0:
if len(self.yellow_titles) != 0:
self.add_locations_info(f, self.yellow_lats, self.yellow_longs,
self.yellow_titles, 'pins/yellow.png', 'yellow')
elif len(self.yellow_titles) == 0:
self.add_locations_markers(f, self.yellow_lats, self.yellow_longs, 'pins/yellow.png', 'yellow')
def set_blue_markers(self, f):
if (len(self.blue_lats) == len(self.blue_longs)) and (len(self.blue_lats) and len(self.blue_longs)) != 0:
if len(self.blue_titles) != 0:
self.add_locations_info(f, self.blue_lats, self.blue_longs,
self.blue_titles, 'pins/blue.png', 'blue')
elif len(self.blue_titles) == 0:
self.add_locations_markers(f, self.blue_lats, self.blue_longs, 'pins/blue.png', 'blue')
def set_black_markers(self, f):
if len(self.black_lats) == len(self.black_longs) and (len(self.black_lats) and len(self.black_longs)) != 0:
if len(self.black_titles) != 0:
self.add_locations_info(f, self.black_lats, self.black_longs,
self.black_titles, 'pins/black.png', 'black')
elif len(self.black_titles) == 0:
self.add_locations_markers(f, self.black_lats, self.black_longs, 'pins/black.png', 'black')
def set_green_markers(self, f):
if len(self.green_lats) == len(self.green_longs) and (len(self.green_lats) and len(self.green_longs)) != 0:
if len(self.green_titles) != 0:
self.add_locations_info(f, self.green_lats, self.green_longs,
self.green_titles, 'pins/green.png', 'green')
elif len(self.green_titles) == 0:
self.add_locations_markers(f, self.green_lats, self.green_longs, 'pins/green.png', 'green')
@staticmethod
def add_locations_info(f, latitudes, longitudes, titles, marker, color):
f.write('\t\t\tvar %s_locations = [\n' % color)
for x in range(len(longitudes)):
if x != len(longitudes) - 1:
f.write('\t\t\t\t[%f, %f, \'%s\'], \n' % (float(latitudes[x]), float(longitudes[x]), titles[x]))
else:
f.write('\t\t\t\t[%f, %f, \'%s\'] \n' % (float(latitudes[x]), float(longitudes[x]), titles[x]))
f.write('\t\t\t];\n')
f.write('\n')
f.write('\t\t\tvar %s_infowindow = new google.maps.InfoWindow();\n' % color)
f.write('\t\t\tvar %s_marker, i;\n' % color)
f.write('\t\t\tfor (i = 0; i < %s_locations.length; i++) {\n' % color)
f.write('\t\t\t\t%s_marker = new google.maps.Marker({\n' % color)
f.write('\t\t\t\t\tposition: new google.maps.LatLng(%s_locations[i][0], %s_locations[i][1]),\n'%(color, color))
f.write('\t\t\t\t\tanimation: google.maps.Animation.DROP,\n')
f.write('\t\t\t\t\tmap: map,\n')
f.write('\t\t\t\t\ticon: \'%s\'\n' % marker)
f.write('\t\t\t\t});\n')
f.write('\n')
f.write('\t\t\t\tgoogle.maps.event.addListener(%s_marker, \'click\', (function (%s_marker, i) {\n' % (color,color))
f.write('\t\t\t\t\treturn function () {\n')
f.write('\t\t\t\t\t\t%s_infowindow.setContent(%s_locations[i][2]);\n' % (color, color))
f.write('\t\t\t\t\t\t%s_infowindow.open(map, %s_marker);\n' % (color, color))
f.write('\t\t\t\t\t}\n')
f.write('\t\t\t\t})(%s_marker, i));\n' % color)
f.write('\t\t\t}\n\n')
@staticmethod
def add_locations_markers(f, latitudes, longitudes, marker, color):
f.write('\t\t\tvar %s_locations = [\n' % color)
for x in range(len(longitudes)):
if x != len(longitudes) - 1:
f.write('\t\t\t\t[%f, %f], \n' % (float(latitudes[x]), float(longitudes[x])))
else:
f.write('\t\t\t\t[%f, %f] \n' % (float(latitudes[x]), float(longitudes[x])))
f.write('\t\t\t];\n')
f.write('\n')
f.write('\t\t\tvar %s_marker, i;\n' % color)
f.write('\t\t\tfor (i = 0; i < %s_locations.length; i++) {\n' % color)
f.write('\t\t\t\t%s_marker = new google.maps.Marker({\n' % color)
f.write(
'\t\t\t\t\tposition: new google.maps.LatLng(%s_locations[i][0], %s_locations[i][1]),\n' % (color, color))
f.write('\t\t\t\t\tanimation: google.maps.Animation.DROP,\n')
f.write('\t\t\t\t\tmap: map,\n')
f.write('\t\t\t\t\ticon: \'%s\'\n' % marker)
f.write('\t\t\t\t});\n')
f.write('\t\t\t}\n')
f.write('\n')
#Example -> creates 'test.html'
# if __name__ == '__main__':
# black_lat = ['49.284040', '49.283190']
# black_lon = ['-123.115950', '-123.115680']
# black_titles = ['black 1', 'black 2']
# red_lat = ['49.283460', '49.284490', '49.284740']
# red_lon = ['-123.116560', '-123.115000', '-123.115170']
# red_titles = ['red 1', 'red 2', 'red 3']
# yellow_lat = ['49.284740', '49.284790', '49.284580', '49.284580']
# yellow_lon = ['-123.115170', '-123.115240', '-123.114610', '-123.117080']
# yellow_titles = ['yellow 1', 'yellow 2', 'yellow 3', 'yellow 4']
# red_lat2 = ['49.284790', '49.285050']
# red_lon2 = ['-123.116780', '-123.116350']
# red_titles2 = ['red 4', 'red 5']
# test = CreateGoogleMapMarkers(49.283140, -123.115950, 16, 'default', '<b>Current Location!</b>')
# test.set_api_key('API_KEY')
# test.create_markers(red_lat, red_lon, red_titles, 'red')
# test.create_markers(black_lat, black_lon, 'None', 'black')
# test.create_markers(yellow_lat, yellow_lon, yellow_titles, 'yellow')
# test.create_markers(red_lat2, red_lon2, red_titles2, 'red')
# test.map_markers('test')