hellope

Javascript Routeboxer

Jan 7th, 2017
1,211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function route() {
  2.   // Clear any previous route boxes from the map
  3.   clearBoxes();
  4.      
  5.   // Convert the distance to box around the route from miles to km
  6.   distance = parseFloat(document.getElementById("distance").value) * 1.609344;
  7.      
  8.   var request = {
  9.     origin: "Surabaya",
  10.     destination: "Sidoarjo",
  11.     travelMode: google.maps.DirectionsTravelMode.DRIVING
  12.   }
  13.      
  14.   // Make the directions request
  15.   directionService.route(request, function(result, status) {
  16.     if (status == google.maps.DirectionsStatus.OK) {
  17.       directionsRenderer.setDirections(result);
  18.          
  19.       // Box around the overview path of the first route
  20.       var path = result.routes[0].overview_path;
  21.       var boxes = routeBoxer.box(path, distance);
  22.       drawBoxes(boxes);
  23.       findPlaces(boxes);
  24.     } else {
  25.       alert("Directions query failed: " + status);
  26.     }
  27.   });
  28. }
  29.    
  30. // Draw the array of boxes as polylines on the map
  31. function drawBoxes(boxes) {
  32.   boxpolys = new Array(boxes.length);
  33.     for (var i = 0; i < boxes.length; i++) {
  34.       boxpolys[i] = new google.maps.Rectangle({
  35.         bounds: boxes[i],
  36.         fillOpacity: 0,
  37.         strokeOpacity: 1.0,
  38.         strokeColor: '#000000',
  39.         strokeWeight: 1,
  40.         map: map
  41.     });
  42.   }
  43. }
  44.    
  45. // Clear boxes currently on the map
  46. function clearBoxes() {
  47.   if (boxpolys != null) {
  48.     for (var i = 0; i < boxpolys.length; i++) {
  49.       boxpolys[i].setMap(null);
  50.     }
  51.   }
  52.   boxpolys = null;
  53. }
  54.  
  55. //placing a marker on the map
  56. function createMarker(map, coords, title) {
  57.   var marker = new google.maps.Marker({
  58.     position: coords,
  59.     map: map,
  60.     title: title,
  61.     draggable: false,
  62.     icon: image
  63.   });
  64.   return marker;
  65. }
  66.  
  67. //Search places having coordinates inside the boxes
  68. function findPlaces(boxes) {
  69.   $.getJSON('proses.php', function(results){
  70.   console.log(results);
  71.   for (var i = 0; i < boxes.length; i++) {
  72.     $.each(results, function(k, v){
  73.         if ( v.lat < boxes[i].getNorthEast().lat() && v.lat > boxes[i].getSouthWest().lat() && v.lng > boxes[i].getSouthWest().lng() && v.lng < boxes[i].getNorthEast().lng()) {
  74.  
  75.           var coords = new google.maps.LatLng(v.lat, v.lng);
  76.           createMarker(map, coords, v.nama);          
  77.         }
  78.     });
  79.   }
  80. });
Advertisement
Add Comment
Please, Sign In to add comment