-
Notifications
You must be signed in to change notification settings - Fork 0
/
schools.html
169 lines (129 loc) · 5.61 KB
/
schools.html
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
---
layout: default
---
{% assign schools = site.data.schools %}
<style>
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 680px;
}
/* Just use .gm-style-iw */
.gm-style-iw {
width: 300px;
min-height: 150px;
}
</style>
{% comment %}
TODO:
- when clicking another link remove previous infowindow
- resize infowindows to fit
- make better icons
- use places API to fill in detailed info
- change colors to match
{% endcomment %}
<div class="list-group mb-4 d-flex flex-row flex-wrap">
<h1>Schools we're working with:</h1>
{% for school in schools %}
<a class="place-{{ school.placeId }} marker-trigger list-group-item list-group-item-action w-50" href="#" data-placeid="{{ school.placeId }}" data-name="{{ school.name }}">{{ school.name }}</a>
{% endfor %}
{% comment %} construct list of school links that trigger info window {% endcomment %}
</div>
<div id="map" class="mb-4"></div>
<script>
function initMap() {
var map = new google.maps.Map(document.getElementById('map'))
var geocoder = new google.maps.Geocoder;
schools = { }
{% for school in schools %}
schools['{{ school.placeId }}'] = '{{ school.name }}'
{% endfor %}
var promisedResults = []
for (let i in schools) {
promisedResults.push(getResByPlaceId(i, geocoder, map));
}
Promise.all(promisedResults).then(function(resolvedResults){
var bounds = new google.maps.LatLngBounds(); // boundaries for all coords of each marker
var locations = []
var markers = {}
resolvedResults.forEach(function(res) {
let latlng = {lat: res[0].geometry.location.lat(), lng: res[0].geometry.location.lng()} // get coords for each res
let name = schools[res[0].place_id] // may be a good use for Places
{% comment %} console.log(res[0]) {% endcomment %}
let county = res[0].address_components[2];
let state = res[0].address_components[4];
var countyStateStr = "";
if (county == undefined || state == undefined) {
countyStateStr = '';
} else {
countyStateStr += county.short_name;
countyStateStr += ', ';
countyStateStr += state.short_name;
}
let contentString = '<div id="content">' +
"<div style='float:left'><img src='//i.stack.imgur.com/g672i.png'></div>" +
"<div style='float:right; padding: 10px;'><b>" + name + "</b><br/>" + countyStateStr +
'</div>'; // to be changed
let infowindow = new google.maps.InfoWindow({
content: contentString, // construct info window
// maxWidth: 200
});
let marker = new google.maps.Marker({ // construct marker and attach map
map: map,
position: latlng
})
marker.addListener('click', function() {
// infoWindow.close();
infowindow.open(map, marker); // add default click listener to each marker
map.panTo(this.getPosition());
map.setZoom(15);
let currentPlace = res[0].place_id;
$('.active').toggleClass('active');
$('.place-' + currentPlace).toggleClass('active');
});
markers[res[0].place_id] = marker; // construct dictionary of place_id => markers
locations.push(latlng); // construct the array of latlng coords
});
$(".marker-trigger").on("click", function() {
let marker = markers[$(this).data("placeid")] // retrieve the corresponding marker based on the buttons place id
google.maps.event.trigger(marker, 'click'); // trigger the info window to open
})
for (var m in locations) {
bounds.extend(locations[m]); // construct boundaries
}
map.setCenter(bounds.getCenter()); // recenter view around all markers based on constructed boundaries
{% comment %} google.maps.event.addListenerOnce(map, 'bounds_changed', function(event) {
this.setZoom(map.getZoom()-1);
if (this.getZoom() > 15) {
this.setZoom(15);
}
}); {% endcomment %}
map.fitBounds(bounds); // resize to fit map to constructed boundaries
})
}
function getResByAddress(address, geocoder) {
return new Promise(function(resolve, reject) {
geocoder.geocode({'address': address}, function(results, status) {
if (status === 'OK') {
resolve(results);
} else {
reject(new Error());
}
})
})
}
function getResByPlaceId(placeId, geocoder, callback) {
return new Promise(function(resolve, reject) {
geocoder.geocode({'placeId': placeId}, function(results, status) {
if (status === 'OK') {
resolve(results);
} else {
reject(new Error());
}
})
})
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key={{ site.google_api_key }}&callback=initMap">
</script>