-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
314 lines (265 loc) · 8.21 KB
/
scripts.js
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/* global google */
/* global _ */
/**
* scripts.js
*
* Computer Science 50
* Problem Set 8
*
* Global JavaScript.
*/
// Google Map
var map;
// markers for map
var markers = [];
// info window
var info = new google.maps.InfoWindow();
// execute when the DOM is fully loaded
$(function() {
// styles for map
// https://developers.google.com/maps/documentation/javascript/styling
var styles = [
// hide Google's labels
{
featureType: "all",
elementType: "labels",
stylers: [
{visibility: "off"}
]
},
// hide roads
{
featureType: "road",
elementType: "geometry",
stylers: [
{visibility: "off"}
]
}
];
// options for map
// https://developers.google.com/maps/documentation/javascript/reference#MapOptions
var options = {
center: {lat: 42.3770, lng: -71.1256}, // Cambridge, Massachussets
disableDefaultUI: true,
mapTypeId: google.maps.MapTypeId.ROADMAP,
maxZoom: 14,
panControl: true,
styles: styles,
zoom: 13,
zoomControl: true
};
// get DOM node in which map will be instantiated
var canvas = $("#map-canvas").get(0);
// instantiate map
map = new google.maps.Map(canvas, options);
// configure UI once Google Map is idle (i.e., loaded)
google.maps.event.addListenerOnce(map, "idle", configure);
});
/**
* Adds marker for place to map.
*/
function addMarker(place)
{
/* create a new variable that has a key of google.maps.LatLng
that is equal to the latitude, and longitude values of the place object */
var myLatLng = new google.maps.LatLng(place.latitude, place.longitude);
// create a new marker with a label
var marker = new MarkerWithLabel({
icon: "http://www.clker.com/cliparts/y/v/I/R/i/4/newspaper-th.png",
position: myLatLng,
draggable: false,
map: map,
labelContent: place.place_name + ", " + place.admin_name1,
labelAnchor: new google.maps.Point(22,0)
});
/* define an object called parameter who's geo key (which is required via
the articles.php file) is the postal code key of the place object that was
passed into the add marker function */
var parameters = {
geo: place.postal_code
};
// add event listener to the marker and info window
google.maps.event.addListener(marker, "click", function ()
{
// use jquery to access the articles.php file
$.getJSON("articles.php", parameters).done(function(data, textStatus, jqXHR)
{
// if there are no articles from articles.php
if(data.length === 0)
{
showInfo(marker, "Slow news day!");
}
// else, show articles in infowindow
else
{
// start unordered list as per showInfo
var ul = "<h4>Local News</h4><ul style=\"list-style-type:none;\">";
// use underscore to create a template for each list item * blank opens in new tab *
var template = _.template("<li><a target=\"_blank\" href =\"<%- link %>\"><%-title%></a></li>");
for (var i = 0, n = data.length ; i < n ; i++)
{
// dynamically add html unordered list to page
// data is an array of associative arrays with link and title keys
ul += template({
link: data[i].link,
title: data[i].title
});
}
// make sure there is a closing unordered list tag
ul += "</ul>";
// pass in marker and content (as ul) to showInfo
showInfo(marker, ul);
}
});
});
// add the marker to the global variable markers[]
markers.push(marker);
}
/**
* Configures application.
*/
function configure()
{
// update UI after map has been dragged
google.maps.event.addListener(map, "dragend", function() {
update();
});
// update UI after zoom level changes
google.maps.event.addListener(map, "zoom_changed", function() {
update();
});
// remove markers whilst dragging
google.maps.event.addListener(map, "dragstart", function() {
removeMarkers();
});
// configure typeahead
// https://github.com/twitter/typeahead.js/blob/master/doc/jquery_typeahead.md
$("#q").typeahead({
autoselect: true,
highlight: true,
minLength: 1
},
{
source: search,
templates: {
empty: "no places found yet",
suggestion: _.template("<p><%- place_name %>, <%- admin_name1 %>, <%- postal_code %></p>")
}
});
// re-center map after place is selected from drop-down
$("#q").on("typeahead:selected", function(eventObject, suggestion, name) {
// ensure coordinates are numbers
var latitude = (_.isNumber(suggestion.latitude)) ? suggestion.latitude : parseFloat(suggestion.latitude);
var longitude = (_.isNumber(suggestion.longitude)) ? suggestion.longitude : parseFloat(suggestion.longitude);
// set map's center
map.setCenter({lat: latitude, lng: longitude});
// update UI
update();
});
// hide info window when text box has focus
$("#q").focus(function(eventData) {
hideInfo();
});
// re-enable ctrl- and right-clicking (and thus Inspect Element) on Google Map
// https://chrome.google.com/webstore/detail/allow-right-click/hompjdfbfmmmgflfjdlnkohcplmboaeo?hl=en
document.addEventListener("contextmenu", function(event) {
event.returnValue = true;
event.stopPropagation && event.stopPropagation();
event.cancelBubble && event.cancelBubble();
}, true);
// update UI
update();
// give focus to text box
$("#q").focus();
}
/**
* Hides info window.
*/
function hideInfo()
{
info.close();
}
/**
* Removes markers from map.
*/
function removeMarkers()
{
/*since each marker was pushed to the global variable markers array,
iterate over the markers array and remove each marker */
for (var i = 0, n = markers.length ; i < n ; i++)
{
markers[i].setMap(null);
}
}
/**
* Searches database for typeahead's suggestions.
*/
function search(query, cb)
{
// get places matching query (asynchronously)
var parameters = {
geo: query
};
$.getJSON("search.php", parameters)
.done(function(data, textStatus, jqXHR) {
// call typeahead's callback with search results (i.e., places)
cb(data);
})
.fail(function(jqXHR, textStatus, errorThrown) {
// log error to browser's console
console.log(errorThrown.toString());
});
}
/**
* Shows info window at marker with content.
*/
function showInfo(marker, content)
{
// start div
var div = "<div id='info'>";
if (typeof(content) === "undefined")
{
// http://www.ajaxload.info/
div += "<img alt='loading' src='img/ajax-loader.gif'/>";
}
else
{
div += content;
}
// end div
div += "</div>";
// set info window's content
info.setContent(div);
// open info window (if not already open)
info.open(map, marker);
}
/**
* Updates UI's markers.
*/
function update()
{
// get map's bounds
var bounds = map.getBounds();
var ne = bounds.getNorthEast();
var sw = bounds.getSouthWest();
// get places within bounds (asynchronously)
var parameters = {
ne: ne.lat() + "," + ne.lng(),
q: $("#q").val(),
sw: sw.lat() + "," + sw.lng()
};
$.getJSON("update.php", parameters)
.done(function(data, textStatus, jqXHR) {
// remove old markers from map
removeMarkers();
// add new markers to map
for (var i = 0; i < data.length; i++)
{
addMarker(data[i]);
}
})
.fail(function(jqXHR, textStatus, errorThrown) {
// log error to browser's console
console.log(errorThrown.toString());
});
}