Advertisement
Guest User

scripts.js

a guest
Aug 14th, 2017
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. // execute when the DOM is fully loaded
  11. $(function() {
  12.  
  13.     // styles for map
  14.     // https://developers.google.com/maps/documentation/javascript/styling
  15.     var styles = [
  16.  
  17.         // hide Google's labels
  18.         {
  19.             featureType: "all",
  20.             elementType: "labels",
  21.             stylers: [
  22.                 {visibility: "off"}
  23.             ]
  24.         },
  25.  
  26.         // hide roads
  27.         {
  28.             featureType: "road",
  29.             elementType: "geometry",
  30.             stylers: [
  31.                 {visibility: "off"}
  32.             ]
  33.         }
  34.  
  35.     ];
  36.  
  37.     // options for map
  38.     // https://developers.google.com/maps/documentation/javascript/reference#MapOptions
  39.     var options = {
  40.         center: {lat: 35.7640, lng: -78.7786}, // Cary, North Carolina
  41.         disableDefaultUI: true,
  42.         mapTypeId: google.maps.MapTypeId.ROADMAP,
  43.         maxZoom: 14,
  44.         panControl: true,
  45.         styles: styles,
  46.         zoom: 13,
  47.         zoomControl: true
  48.     };
  49.  
  50.     // get DOM node in which map will be instantiated
  51.     var canvas = $("#map-canvas").get(0);
  52.  
  53.     // instantiate map
  54.     map = new google.maps.Map(canvas, options);
  55.  
  56.     // configure UI once Google Map is idle (i.e., loaded)
  57.     google.maps.event.addListenerOnce(map, "idle", configure);
  58.  
  59. });
  60.  
  61. /**
  62.  * Adds marker for place to map.
  63.  */
  64. function addMarker(place)
  65. {
  66.     var url = "/search?q=" + place;
  67.     $.getJSON(url, function(data){
  68.         var location = data.latitude + data.longitude;
  69.         mark = new google.maps.marker(location, map);
  70.         markers[0] = mark;
  71.     });
  72.    
  73. }
  74.  
  75. /**
  76.  * Configures application.
  77.  */
  78. function configure()
  79. {
  80.     // update UI after map has been dragged
  81.     google.maps.event.addListener(map, "dragend", function() {
  82.  
  83.         // if info window isn't open
  84.         // http://stackoverflow.com/a/12410385
  85.         if (!info.getMap || !info.getMap())
  86.         {
  87.             update();
  88.         }
  89.     });
  90.  
  91.     // update UI after zoom level changes
  92.     google.maps.event.addListener(map, "zoom_changed", function() {
  93.         update();
  94.     });
  95.  
  96.     // configure typeahead
  97.     $("#q").typeahead({
  98.         highlight: false,
  99.         minLength: 1
  100.     },
  101.     {
  102.         display: function(suggestion) { return null; },
  103.         limit: 10,
  104.         source: search,
  105.         templates: {
  106.             suggestion: Handlebars.compile(
  107.                 "<div>" +
  108.                 "{{ place_name }}, {{ admin_name1 }}, {{ postal_code }}" +
  109.                 "</div>"
  110.             )
  111.         }
  112.     });
  113.  
  114.     // re-center map after place is selected from drop-down
  115.     $("#q").on("typeahead:selected", function(eventObject, suggestion, name) {
  116.  
  117.         // set map's center
  118.         map.setCenter({lat: parseFloat(suggestion.latitude), lng: parseFloat(suggestion.longitude)});
  119.  
  120.         // update UI
  121.         update();
  122.     });
  123.  
  124.     // hide info window when text box has focus
  125.     $("#q").focus(function(eventData) {
  126.         info.close();
  127.     });
  128.  
  129.     // re-enable ctrl- and right-clicking (and thus Inspect Element) on Google Map
  130.     // https://chrome.google.com/webstore/detail/allow-right-click/hompjdfbfmmmgflfjdlnkohcplmboaeo?hl=en
  131.     document.addEventListener("contextmenu", function(event) {
  132.         event.returnValue = true;
  133.         event.stopPropagation && event.stopPropagation();
  134.         event.cancelBubble && event.cancelBubble();
  135.     }, true);
  136.  
  137.     // update UI
  138.     update();
  139.  
  140.     // give focus to text box
  141.     $("#q").focus();
  142. }
  143.  
  144. /**
  145.  * Removes markers from map.
  146.  */
  147. function removeMarkers()
  148. {
  149.     // TODO
  150. }
  151.  
  152. /**
  153.  * Searches database for typeahead's suggestions.
  154.  */
  155. function search(query, syncResults, asyncResults)
  156. {
  157.     // get places matching query (asynchronously)
  158.     var parameters = {
  159.         q: query
  160.     };
  161.     $.getJSON(Flask.url_for("search"), parameters)
  162.     .done(function(data, textStatus, jqXHR) {
  163.      
  164.         // call typeahead's callback with search results (i.e., places)
  165.         asyncResults(data);
  166.     })
  167.     .fail(function(jqXHR, textStatus, errorThrown) {
  168.  
  169.         // log error to browser's console
  170.         console.log(errorThrown.toString());
  171.  
  172.         // call typeahead's callback with no results
  173.         asyncResults([]);
  174.     });
  175. }
  176.  
  177. /**
  178.  * Shows info window at marker with content.
  179.  */
  180. function showInfo(marker, content)
  181. {
  182.     // start div
  183.     var div = "<div id='info'>";
  184.     if (typeof(content) == "undefined")
  185.     {
  186.         // http://www.ajaxload.info/
  187.         div += "<img alt='loading' src='/static/ajax-loader.gif'/>";
  188.     }
  189.     else
  190.     {
  191.         div += content;
  192.     }
  193.  
  194.     // end div
  195.     div += "</div>";
  196.  
  197.     // set info window's content
  198.     info.setContent(div);
  199.  
  200.     // open info window (if not already open)
  201.     info.open(map, marker);
  202. }
  203.  
  204. /**
  205.  * Updates UI's markers.
  206.  */
  207. function update()
  208. {
  209.     // get map's bounds
  210.     var bounds = map.getBounds();
  211.     var ne = bounds.getNorthEast();
  212.     var sw = bounds.getSouthWest();
  213.  
  214.     // get places within bounds (asynchronously)
  215.     var parameters = {
  216.         ne: ne.lat() + "," + ne.lng(),
  217.         q: $("#q").val(),
  218.         sw: sw.lat() + "," + sw.lng()
  219.     };
  220.     $.getJSON(Flask.url_for("update"), parameters)
  221.     .done(function(data, textStatus, jqXHR) {
  222.  
  223.        // remove old markers from map
  224.        removeMarkers();
  225.  
  226.        // add new markers to map
  227.        for (var i = 0; i < data.length; i++)
  228.        {
  229.            addMarker(data[i]);
  230.        }
  231.     })
  232.     .fail(function(jqXHR, textStatus, errorThrown) {
  233.  
  234.         // log error to browser's console
  235.         console.log(errorThrown.toString());
  236.     });
  237. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement