Advertisement
shadiff

revert2.js

Jun 21st, 2023
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.84 KB | None | 0 0
  1. var orderList = []; // Array to store orders
  2. var itemNameList = [] //Array to store Item Names
  3. var totalPrice = 0; // Variable to store the total price
  4.  
  5. function getDistance(callback) {
  6. const map = new google.maps.Map(document.getElementById("map"), {
  7. center: { lat: -1.286389, lng: 36.817223 },
  8. zoom: 13,
  9. mapTypeId: "roadmap",
  10. });
  11. var geocoder = new google.maps.Geocoder();
  12.  
  13. const destinationA = document.getElementById('origin-input').value;
  14. const destinationB = document.getElementById('destination-input').value;
  15.  
  16. var originLat = "";
  17. var originLon = "";
  18. var destinationLat = "";
  19. var destinationLon = "";
  20. geocoder.geocode({ 'address': destinationA }, function (results, status) {
  21. if (status == google.maps.GeocoderStatus.OK) {
  22. originLat = results[0].geometry.location.lat();
  23. originLon = results[0].geometry.location.lng();
  24.  
  25. geocoder.geocode({ 'address': destinationB }, function (results, status) {
  26. if (status == google.maps.GeocoderStatus.OK) {
  27. destinationLat = results[0].geometry.location.lat();
  28. destinationLon = results[0].geometry.location.lng();
  29.  
  30. console.log("Origin");
  31. console.log(originLat);
  32. console.log(originLon);
  33.  
  34. console.log("Destination");
  35. console.log(destinationLat);
  36. console.log(destinationLon);
  37.  
  38. addDirections(
  39. map,
  40. new google.maps.LatLng(originLat, originLon),
  41. new google.maps.LatLng(destinationLat, destinationLon)
  42. );
  43.  
  44. // Initialize services
  45. const service = new google.maps.DistanceMatrixService();
  46. const origin = new google.maps.LatLng(originLat, originLon);
  47. const destination = new google.maps.LatLng(destinationLat, destinationLon);
  48. const request = {
  49. origins: [origin],
  50. destinations: [destination],
  51. travelMode: google.maps.TravelMode.DRIVING,
  52. unitSystem: google.maps.UnitSystem.METRIC,
  53. avoidHighways: false,
  54. avoidTolls: false,
  55. };
  56.  
  57. service.getDistanceMatrix(request).then((response) => {
  58. var distance = response['rows'][0]['elements'][0]['distance']['text'];
  59. distance=distance.split(" ");
  60. distance=distance[0];
  61.  
  62. console.log("Total Distance is = " + distance);
  63. document.getElementById('distance-field').value = distance; // Set the value of the hidden field
  64. document.getElementById('origin_latlng-field').value = originLat + "," + originLon;
  65. document.getElementById('delivery_latlng-field').value = destinationLat + "," + destinationLon
  66.  
  67.  
  68. callback(distance); // Invoke the callback with the distance value
  69. });
  70. }
  71. });
  72. }
  73. });
  74. }
  75.  
  76. function getDistance(callback) {
  77. const map = new google.maps.Map(document.getElementById("map"), {
  78. center: { lat: -1.286389, lng: 36.817223 },
  79. zoom: 13,
  80. mapTypeId: "roadmap",
  81. });
  82. var geocoder = new google.maps.Geocoder();
  83.  
  84. const destinationA = document.getElementById('origin-input').value;
  85. const destinationB = document.getElementById('destination-input').value;
  86.  
  87. var originLat = "";
  88. var originLon = "";
  89. var destinationLat = "";
  90. var destinationLon = "";
  91. geocoder.geocode({ 'address': destinationA }, function (results, status) {
  92. if (status == google.maps.GeocoderStatus.OK) {
  93. originLat = results[0].geometry.location.lat();
  94. originLon = results[0].geometry.location.lng();
  95.  
  96. geocoder.geocode({ 'address': destinationB }, function (results, status) {
  97. if (status == google.maps.GeocoderStatus.OK) {
  98. destinationLat = results[0].geometry.location.lat();
  99. destinationLon = results[0].geometry.location.lng();
  100.  
  101. console.log("Origin");
  102. console.log(originLat);
  103. console.log(originLon);
  104.  
  105. console.log("Destination");
  106. console.log(destinationLat);
  107. console.log(destinationLon);
  108.  
  109. addDirections(
  110. map,
  111. new google.maps.LatLng(originLat, originLon),
  112. new google.maps.LatLng(destinationLat, destinationLon)
  113. );
  114.  
  115. // Initialize services
  116. const service = new google.maps.DistanceMatrixService();
  117. const origin = new google.maps.LatLng(originLat, originLon);
  118. const destination = new google.maps.LatLng(destinationLat, destinationLon);
  119. const request = {
  120. origins: [origin],
  121. destinations: [destination],
  122. travelMode: google.maps.TravelMode.DRIVING,
  123. unitSystem: google.maps.UnitSystem.METRIC,
  124. avoidHighways: false,
  125. avoidTolls: false,
  126. };
  127.  
  128. service.getDistanceMatrix(request).then((response) => {
  129. var distance = response['rows'][0]['elements'][0]['distance']['text'];
  130. distance=distance.split(" ");
  131. distance=distance[0];
  132.  
  133. console.log("Total Distance is = " + distance);
  134. document.getElementById('distance-field').value = distance; // Set the value of the hidden field
  135. document.getElementById('origin_latlng-field').value = originLat + "," + originLon;
  136. document.getElementById('delivery_latlng-field').value = destinationLat + "," + destinationLon
  137.  
  138.  
  139. callback(distance); // Invoke the callback with the distance value
  140. });
  141. }
  142. });
  143. }
  144. });
  145. }
  146. function submitOrder(distance) {
  147. var orderItemInput = document.getElementById("orderItemInput");
  148. var ItemNameInput = document.getElementById("ItemNameInput")
  149. var orderPackageSelect = document.getElementById("orderPackageSelect");
  150. var resultDiv = document.getElementById("result");
  151. var priceDiv = document.getElementById("price");
  152.  
  153. var orderItem = orderItemInput.value;
  154. var itemName = ItemNameInput.value;
  155. var orderPackage = orderPackageSelect.value;
  156.  
  157. // Calculate the price based on the selected package and distance
  158. var price = calculatePrice(orderPackage, distance);
  159.  
  160. // Add the order to the orderList array
  161. orderList.push({
  162. orderItem: orderItem,
  163. orderPackage: orderPackage,
  164. price: price
  165. });
  166.  
  167. // Add the ItemName to the NameList array
  168. itemNameList.push({
  169. itemName : itemName
  170. })
  171.  
  172.  
  173. // Update the total price by adding the price of the current order
  174. totalPrice += price;
  175.  
  176. // Display all orders in the resultDiv
  177. resultDiv.textContent = "";
  178. priceDiv.textContent = "";
  179.  
  180. for (var i = 0; i < orderList.length; i++) {
  181. resultDiv.innerHTML += "Order " + (i + 1) + ": Item - " + orderList[i].orderItem + ", Package - " + orderList[i].orderPackage + "<br>";
  182. priceDiv.innerHTML += "Order " + (i + 1) + ": Price - Kes" + orderList[i].price.toFixed(2) + "<br>"+ "<input name='amount[]' type='text' value='"+orderList[i].price.toFixed(2)+"' />";
  183.  
  184. }
  185. for (var j = 0; j < itemName.length; j++) {
  186. itemName.innerHTML += "Item " + (j + 1) + ": " + itemName[j] + "<br>";
  187.  
  188. }
  189. // Display all ItemNames in the Namediv
  190. ItemNameInput.textContent = "";
  191.  
  192.  
  193.  
  194. // Update the display of the total price
  195. document.getElementById("totalPrice").textContent = "Total Price: Kes" + totalPrice.toFixed(2);
  196.  
  197. document.getElementById("orderAmountInput").value = totalPrice;
  198. // Submit the form
  199.  
  200.  
  201. // Clear the input and select fields
  202. orderItemInput.value = "";
  203. orderPackageSelect.value = "";
  204. itemName.value = "";
  205. }
  206.  
  207. function removeOrder() {
  208. var resultDiv = document.getElementById("result");
  209. var priceDiv = document.getElementById("price");
  210. var itemNameDiv = document.getElementById("ItemNameInput");
  211.  
  212. if (orderList.length > 0) {
  213. // Get the price of the last order
  214. var removedOrderPrice = orderList[orderList.length - 1].price;
  215.  
  216. // Subtract the price of the removed order from the total price
  217. totalPrice -= removedOrderPrice;
  218.  
  219. // Remove the last order from the orderList array
  220. orderList.pop();
  221.  
  222. // Display the updated list of orders and prices
  223. resultDiv.textContent = "";
  224. priceDiv.textContent = "";
  225. itemNameDiv.textContent = "";
  226.  
  227. for (var i = 0; i < orderList.length; i++) {
  228. resultDiv.innerHTML += "Order " + (i + 1) + ": Item - " + orderList[i].orderItem + ", Package - " + orderList[i].orderPackage + "<br>";
  229. priceDiv.innerHTML += "Order " + (i + 1) + ": Price - Kes" + orderList[i].price.toFixed(2) + "<br>";
  230.  
  231. document.getElementById("orderAmountInput").value = orderList[i].price.toFixed(2);
  232. }
  233. for (var j = 0; j < itemName.length; j++) {
  234. itemNameDiv.innerHTML += "Item " + (j + 1) + ": " + itemName[j] + "<br>";
  235.  
  236. }
  237. }
  238.  
  239. // Update the display of the total price
  240. document.getElementById("totalPrice").textContent = "Total Price: Kes " + totalPrice.toFixed(2);
  241. }
  242.  
  243. function calculateDistancePrice(distance) {
  244. const basePrice = 500; // Base price for the first 5 km
  245. const extraPricePerKm = 50; // Additional price per km beyond the first 10 km
  246.  
  247. if (distance <= 5) {
  248. return basePrice;
  249. } else {
  250. const extraDistance = distance - 5;
  251. const extraPrice = extraDistance * extraPricePerKm;
  252. return basePrice + extraPrice;
  253. }
  254. }
  255.  
  256. function calculatePrice(orderPackage, distance) {
  257. // Add your price calculation logic here based on the selected package and distance
  258. // This is just a placeholder implementation
  259.  
  260. console.log("Order Package: ", orderPackage);
  261. console.log("Distance: ", distance);
  262.  
  263. if (orderPackage === "package1") {
  264. return 100 + calculateDistancePrice(distance);
  265. } else if (orderPackage === "package2") {
  266. return 200 + calculateDistancePrice(distance);
  267. } else if (orderPackage === "package3") {
  268. return 300 + calculateDistancePrice(distance);
  269. } else {
  270. return 0;
  271. }
  272. }
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement