Advertisement
JoshCopeland

pset8 removemarkers() bug

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