Advertisement
callyb

pset8 scripts.js

Apr 29th, 2017
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.48 KB | None | 0 0
  1. // Google Map
  2. var map;
  3.  
  4. // markers for map
  5. var markers = [];
  6.  
  7. // info window
  8. var info = new google.maps.InfoWindow();
  9.  
  10. //set markers function
  11. function setMapOnAll(map) {
  12. for (var i = 0; i < markers.length; i++) {
  13. markers[i].setMap(map);
  14. }
  15. }
  16.  
  17. // execute when the DOM is fully loaded
  18. $(function() {
  19.  
  20. // styles for map
  21. // https://developers.google.com/maps/documentation/javascript/styling
  22. var styles = [
  23.  
  24. // hide Google's labels
  25. {
  26. featureType: "all",
  27. elementType: "labels",
  28. stylers: [
  29. {visibility: "off"}
  30. ]
  31. },
  32.  
  33. // hide roads
  34. {
  35. featureType: "road",
  36. elementType: "geometry",
  37. stylers: [
  38. {visibility: "off"}
  39. ]
  40. }
  41.  
  42. ];
  43.  
  44. // options for map
  45. // https://developers.google.com/maps/documentation/javascript/reference#MapOptions
  46. var options = {
  47. center: {lat: 41.3184, lng: -72.9318}, // New Haven
  48. disableDefaultUI: true,
  49. mapTypeId: google.maps.MapTypeId.ROADMAP,
  50. maxZoom: 14,
  51. panControl: true,
  52. styles: styles,
  53. zoom: 13,
  54. zoomControl: true
  55. };
  56.  
  57.  
  58.  
  59.  
  60. // get DOM node in which map will be instantiated
  61. var canvas = $("#map-canvas").get(0);
  62.  
  63. // instantiate map
  64. map = new google.maps.Map(canvas, options);
  65.  
  66. // configure UI once Google Map is idle (i.e., loaded)
  67. google.maps.event.addListenerOnce(map, "idle", configure);
  68.  
  69. });
  70.  
  71. /**
  72. * Adds marker for place to map.
  73. */
  74. function addMarker(place)
  75. {
  76. var myLatLng = {lat:place.latitude, lng:place.longitude};
  77.  
  78.  
  79. var marker = new google.maps.Marker({
  80. position: myLatLng,
  81. map: map,
  82. label: {
  83. color: 'black',
  84. text: place.place_name,
  85. },
  86. icon: {
  87. labelOrigin: new google.maps.Point(11, 50),
  88. url: 'https://maps.google.com/mapfiles/kml/pal2/icon5.png',
  89. size: new google.maps.Size(30, 60),
  90. origin: new google.maps.Point(0, 0),
  91. anchor: new google.maps.Point(11, 40),
  92. },
  93. });
  94.  
  95. markers.push(marker);
  96. // Sets the map on all markers in the array.
  97.  
  98. setMapOnAll(map);
  99.  
  100.  
  101. var parameters = { geo:place.postal_code };
  102.  
  103.  
  104. $.getJSON(Flask.url_for("articles"), parameters)
  105. .done(function(data, textStatus, jqXHR){
  106.  
  107. //create ul of titles with links to add to div in showInfo as 'content'
  108. // Create the list element:
  109. var list = '<ul>';
  110.  
  111. for(var i = 0; i < data.length; i++) {
  112. // Create the list item:
  113. list += '<li>';
  114.  
  115. //add the link
  116. list += '<a href ="' + data[i].link + '">'
  117.  
  118. // Set its contents & add to list
  119. list += data[i].title;
  120.  
  121. //close the li item
  122. list += '</li>';
  123. }
  124.  
  125. // close the list:
  126. list += '</ul>';
  127.  
  128. //onclick
  129. google.maps.event.addListener(marker, 'click', function(){
  130. // call showInfo
  131. showInfo(marker, list);
  132.  
  133. });
  134.  
  135.  
  136.  
  137. });
  138.  
  139. }
  140.  
  141. /**
  142. * Configures application.
  143. */
  144. function configure()
  145. {
  146. // update UI after map has been dragged
  147. google.maps.event.addListener(map, "dragend", function() {
  148.  
  149. // if info window isn't open
  150. // http://stackoverflow.com/a/12410385
  151. if (!info.getMap || !info.getMap())
  152. {
  153. update();
  154. }
  155. });
  156.  
  157. // update UI after zoom level changes
  158. google.maps.event.addListener(map, "zoom_changed", function() {
  159. update();
  160. });
  161.  
  162. // configure typeahead
  163. $("#q").typeahead({
  164. highlight: false,
  165. minLength: 1
  166. },
  167. {
  168. display: function(suggestion) { return null; },
  169. limit: 10,
  170. source: search,
  171. templates: {
  172. suggestion: Handlebars.compile(
  173. "<div>" +
  174. "{{ place_name }}, {{ admin_name1 }}, {{ postal_code }}" +
  175. "</div>"
  176. )
  177. }
  178. });
  179.  
  180. // re-center map after place is selected from drop-down
  181. $("#q").on("typeahead:selected", function(eventObject, suggestion, name) {
  182.  
  183. // set map's center
  184. map.setCenter({lat: parseFloat(suggestion.latitude), lng: parseFloat(suggestion.longitude)});
  185.  
  186. // update UI
  187. update();
  188. });
  189.  
  190. // hide info window when text box has focus
  191. $("#q").focus(function(eventData) {
  192. info.close();
  193. });
  194.  
  195. // re-enable ctrl- and right-clicking (and thus Inspect Element) on Google Map
  196. // https://chrome.google.com/webstore/detail/allow-right-click/hompjdfbfmmmgflfjdlnkohcplmboaeo?hl=en
  197. document.addEventListener("contextmenu", function(event) {
  198. event.returnValue = true;
  199. event.stopPropagation && event.stopPropagation();
  200. event.cancelBubble && event.cancelBubble();
  201. }, true);
  202.  
  203. // update UI
  204. update();
  205.  
  206. // give focus to text box
  207. $("#q").focus();
  208. }
  209.  
  210. /**
  211. * Removes markers from map.
  212. */
  213. function removeMarkers()
  214. {
  215. setMapOnAll(null);
  216. markers = [];
  217. }
  218.  
  219. /**
  220. * Searches database for typeahead's suggestions.
  221. */
  222. function search(query, syncResults, asyncResults)
  223. {
  224. // get places matching query (asynchronously)
  225. var parameters = {
  226. q: query
  227. };
  228. $.getJSON(Flask.url_for("search"), parameters)
  229. .done(function(data, textStatus, jqXHR) {
  230.  
  231. // call typeahead's callback with search results (i.e., places)
  232. asyncResults(data);
  233. })
  234. .fail(function(jqXHR, textStatus, errorThrown) {
  235.  
  236. // log error to browser's console
  237. console.log(errorThrown.toString());
  238.  
  239. // call typeahead's callback with no results
  240. asyncResults([]);
  241. });
  242. }
  243.  
  244. /**
  245. * Shows info window at marker with content.
  246. */
  247. function showInfo(marker, content)
  248. {
  249.  
  250.  
  251. // start div
  252. var div = "<div id='info'>";
  253. if (typeof(content) == "undefined")
  254. {
  255. // http://www.ajaxload.info/
  256. div += "<img alt='loading' src='/static/ajax-loader.gif'/>";
  257. }
  258. else
  259. {
  260. div += content;
  261. }
  262.  
  263. // end div
  264. div += "</div>";
  265.  
  266. // set info window's content
  267. info.setContent(div);
  268.  
  269. // open info window (if not already open)
  270. info.open(map, marker);
  271. }
  272.  
  273. /**
  274. * Updates UI's markers.
  275. */
  276. function update()
  277. {
  278. // get map's bounds
  279. var bounds = map.getBounds();
  280. var ne = bounds.getNorthEast();
  281. var sw = bounds.getSouthWest();
  282.  
  283. // get places within bounds (asynchronously)
  284. var parameters = {
  285. ne: ne.lat() + "," + ne.lng(),
  286. q: $("#q").val(),
  287. sw: sw.lat() + "," + sw.lng()
  288. };
  289. $.getJSON(Flask.url_for("update"), parameters)
  290. .done(function(data, textStatus, jqXHR) {
  291.  
  292. // remove old markers from map
  293. removeMarkers();
  294.  
  295. // add new markers to map
  296. for (var i = 0; i < data.length; i++)
  297. {
  298. addMarker(data[i]);
  299. }
  300. })
  301. .fail(function(jqXHR, textStatus, errorThrown) {
  302.  
  303. // log error to browser's console
  304. console.log(errorThrown.toString());
  305. });
  306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement