var infowindow = new google.maps.InfoWindow();
var map;
var routeBoxer;
var service;
var delay = 250;

function initialize() {
  directionsDisplay = new google.maps.DirectionsRenderer();
  var mapOptions = {
    zoom: 4,
    center: new google.maps.LatLng(39, -98),
    mapTypeId: google.maps.MapTypeId.ROADMAP,
  };
  map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
  service = new google.maps.places.PlacesService(map);

  routeBoxer = new RouteBoxer();

  directionService = new google.maps.DirectionsService();
  directionsRenderer = new google.maps.DirectionsRenderer({ map: map })
  
  directionsDisplay.setMap(map);
  directionsDisplay.setPanel(document.getElementById('directions-panel'));
}

function calcRoute() {
  var start = document.getElementById('start').value;
  var end = document.getElementById('end').value;

  var waypt1 = document.getElementById('waypoint1').value;
  var waypt2 = document.getElementById('waypoint2').value;
  var waypts = []
  if (waypt1) {
    waypts.push({
      location:waypt1,
      stopover:true});  
  }
  if (waypt2) {
    waypts.push({
      location:waypt2,
      stopover:true});  
  }

  var request = {
    origin: start,
    destination: end,
    waypoints: waypts,
    optimizeWaypoints: true,
    travelMode: google.maps.TravelMode.DRIVING
  };

  directionService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(response);

      // Build boxes around route
      var path = response.routes[0].overview_path;
      var boxes = routeBoxer.box(path, 2); // distance in km from route
      // drawBoxes(boxes);
      var searchIndex = boxes.length - 1;
      queryPlaces(boxes, searchIndex);
    } else {
      alert("Directions query failed: " + status);
    }
  });
}

function queryPlaces(boxes, searchIndex) {
  // delay calls to Places API to prevent going over query limit (10/sec)
  var bounds = boxes[searchIndex];
  findPlaces(bounds);
  findPlacesByText(bounds);
  if (searchIndex > 0) {
    searchIndex--;
    setTimeout(queryPlaces, delay, boxes, searchIndex);
  }
}

function findPlaces(bounds) {
  var selectedTypes = []; 

  var inputElements = document.getElementsByClassName('placeOption');
  
  for (var i=0; inputElements[i]; i++) {
    if (inputElements[i].checked) {
     selectedTypes.push(inputElements[i].value)
    }
  }

  var request = {
    bounds: bounds,
    types: selectedTypes
  };

  if (selectedTypes.length > 0) {
    service.radarSearch(request, function(results, status) {
      if (status == google.maps.places.PlacesServiceStatus.OK) {
        for (var i = 0; i < results.length; i++) {
          createMarker(results[i]);
        }
      } else if (status == google.maps.places.PlacesServiceStatus.OVER_QUERY_LIMIT) {
        delay++;
        console.log('new delay: ' + delay);
        setTimeout(findPlaces, delay, bounds);
      } else {
        console.log('Error: ' + status);
      }
    });
  }
}

function findPlacesByText(bounds) {
  var selectedTypes = ''; 

  var inputElements = document.getElementsByClassName('textOption');
  
  for (var i=0; inputElements[i]; i++) {
    if (inputElements[i].checked) {
     selectedTypes += inputElements[i].value + ', '
    }
  }

  var request = {
    bounds: bounds,
    query: selectedTypes
  };

  if (selectedTypes.length > 0) {
    service.textSearch(request, function(results, status) {
      if (status == google.maps.places.PlacesServiceStatus.OK) {
        for (var i = 0; i < results.length; i++) {
          createMarker(results[i]);
        }
      } else if (status == google.maps.places.PlacesServiceStatus.OVER_QUERY_LIMIT) {
        delay++;
        console.log('new delay: ' + delay);
        findPlacesByText(bounds);
      } else {
        console.log('Error: ' + status);
      }
    });
  }
}

function callback(results, status) {
  if (status == google.maps.places.PlacesServiceStatus.OK) {
    for (var i = 0; i < results.length; i++) {
      createMarker(results[i]);
    }
  } else if (status == google.maps.places.PlacesServiceStatus.OVER_QUERY_LIMIT) {
    delay++;
    console.log(status + ': ' + results);
  } else {
    console.log('Error: ' + status);
  }
}

function createMarker(place) {
  var marker = new google.maps.Marker({
    map: map,
    position: place.geometry.location
  });

  var request = {
    reference: place.reference
  };

  google.maps.event.addListener(marker,'click',function(){
    service.getDetails(request, function(place, status) {
      if (status == google.maps.places.PlacesServiceStatus.OK) {
        var contentStr = '<h5>' + place.name + '</h5><p>' + place.formatted_address;
        if (!!place.formatted_phone_number) contentStr += '<br />' + place.formatted_phone_number;
        if (!!place.website) contentStr += '<br /><a target="_blank" href="' + place.website + '">' + place.website + '</a>';
        contentStr += '<br />' + place.types + '</p>';
        infowindow.setContent(contentStr);
        infowindow.open(map,marker);
      } else {
        var contentStr = "<h5>No Result, status=" + status + "</h5>";
        infowindow.setContent(contentStr);
        infowindow.open(map,marker);
      }
    });
  });
}


function drawBoxes(boxes) {
  boxpolys = new Array(boxes.length);
  for (var i = 0; i < boxes.length; i++) {

    boxpolys[i] = new google.maps.Rectangle({
      bounds: boxes[i],
      fillOpacity: 0,
      strokeOpacity: 1.0,
      strokeColor: '#000000',
      strokeWeight: 1,
      map: map
    });
  }
}

google.maps.event.addDomListener(window, 'load', initialize);

@@ -1652,35 +1652,8 @@
 ute%0A
-      // drawBoxes(boxes);%0A
     
@@ -3848,383 +3848,8 @@
 %0A%7D%0A%0A
-function callback(results, status) %7B%0A  if (status == google.maps.places.PlacesServiceStatus.OK) %7B%0A    for (var i = 0; i %3C results.length; i++) %7B%0A      createMarker(results%5Bi%5D);%0A    %7D%0A  %7D else if (status == google.maps.places.PlacesServiceStatus.OVER_QUERY_LIMIT) %7B%0A    delay++;%0A    console.log(status + ': ' + results);%0A  %7D else %7B%0A    console.log('Error: ' + status);%0A  %7D%0A%7D%0A%0A
 func
@@ -4850,320 +4850,8 @@
 %0A%7D%0A%0A
-%0Afunction drawBoxes(boxes) %7B%0A  boxpolys = new Array(boxes.length);%0A  for (var i = 0; i %3C boxes.length; i++) %7B%0A%0A    boxpolys%5Bi%5D = new google.maps.Rectangle(%7B%0A      bounds: boxes%5Bi%5D,%0A      fillOpacity: 0,%0A      strokeOpacity: 1.0,%0A      strokeColor: '#000000',%0A      strokeWeight: 1,%0A      map: map%0A    %7D);%0A  %7D%0A%7D%0A%0A
 goog

Remove unused functions