Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var orderList = []; // Array to store orders
- var itemNameList = [] //Array to store Item Names
- var totalPrice = 0; // Variable to store the total price
- function getDistance(callback) {
- const map = new google.maps.Map(document.getElementById("map"), {
- center: { lat: -1.286389, lng: 36.817223 },
- zoom: 13,
- mapTypeId: "roadmap",
- });
- var geocoder = new google.maps.Geocoder();
- const destinationA = document.getElementById('origin-input').value;
- const destinationB = document.getElementById('destination-input').value;
- var originLat = "";
- var originLon = "";
- var destinationLat = "";
- var destinationLon = "";
- geocoder.geocode({ 'address': destinationA }, function (results, status) {
- if (status == google.maps.GeocoderStatus.OK) {
- originLat = results[0].geometry.location.lat();
- originLon = results[0].geometry.location.lng();
- geocoder.geocode({ 'address': destinationB }, function (results, status) {
- if (status == google.maps.GeocoderStatus.OK) {
- destinationLat = results[0].geometry.location.lat();
- destinationLon = results[0].geometry.location.lng();
- console.log("Origin");
- console.log(originLat);
- console.log(originLon);
- console.log("Destination");
- console.log(destinationLat);
- console.log(destinationLon);
- addDirections(
- map,
- new google.maps.LatLng(originLat, originLon),
- new google.maps.LatLng(destinationLat, destinationLon)
- );
- // Initialize services
- const service = new google.maps.DistanceMatrixService();
- const origin = new google.maps.LatLng(originLat, originLon);
- const destination = new google.maps.LatLng(destinationLat, destinationLon);
- const request = {
- origins: [origin],
- destinations: [destination],
- travelMode: google.maps.TravelMode.DRIVING,
- unitSystem: google.maps.UnitSystem.METRIC,
- avoidHighways: false,
- avoidTolls: false,
- };
- service.getDistanceMatrix(request).then((response) => {
- var distance = response['rows'][0]['elements'][0]['distance']['text'];
- distance=distance.split(" ");
- distance=distance[0];
- console.log("Total Distance is = " + distance);
- document.getElementById('distance-field').value = distance; // Set the value of the hidden field
- document.getElementById('origin_latlng-field').value = originLat + "," + originLon;
- document.getElementById('delivery_latlng-field').value = destinationLat + "," + destinationLon
- callback(distance); // Invoke the callback with the distance value
- });
- }
- });
- }
- });
- }
- function getDistance(callback) {
- const map = new google.maps.Map(document.getElementById("map"), {
- center: { lat: -1.286389, lng: 36.817223 },
- zoom: 13,
- mapTypeId: "roadmap",
- });
- var geocoder = new google.maps.Geocoder();
- const destinationA = document.getElementById('origin-input').value;
- const destinationB = document.getElementById('destination-input').value;
- var originLat = "";
- var originLon = "";
- var destinationLat = "";
- var destinationLon = "";
- geocoder.geocode({ 'address': destinationA }, function (results, status) {
- if (status == google.maps.GeocoderStatus.OK) {
- originLat = results[0].geometry.location.lat();
- originLon = results[0].geometry.location.lng();
- geocoder.geocode({ 'address': destinationB }, function (results, status) {
- if (status == google.maps.GeocoderStatus.OK) {
- destinationLat = results[0].geometry.location.lat();
- destinationLon = results[0].geometry.location.lng();
- console.log("Origin");
- console.log(originLat);
- console.log(originLon);
- console.log("Destination");
- console.log(destinationLat);
- console.log(destinationLon);
- addDirections(
- map,
- new google.maps.LatLng(originLat, originLon),
- new google.maps.LatLng(destinationLat, destinationLon)
- );
- // Initialize services
- const service = new google.maps.DistanceMatrixService();
- const origin = new google.maps.LatLng(originLat, originLon);
- const destination = new google.maps.LatLng(destinationLat, destinationLon);
- const request = {
- origins: [origin],
- destinations: [destination],
- travelMode: google.maps.TravelMode.DRIVING,
- unitSystem: google.maps.UnitSystem.METRIC,
- avoidHighways: false,
- avoidTolls: false,
- };
- service.getDistanceMatrix(request).then((response) => {
- var distance = response['rows'][0]['elements'][0]['distance']['text'];
- distance=distance.split(" ");
- distance=distance[0];
- console.log("Total Distance is = " + distance);
- document.getElementById('distance-field').value = distance; // Set the value of the hidden field
- document.getElementById('origin_latlng-field').value = originLat + "," + originLon;
- document.getElementById('delivery_latlng-field').value = destinationLat + "," + destinationLon
- callback(distance); // Invoke the callback with the distance value
- });
- }
- });
- }
- });
- }
- function submitOrder(distance) {
- var orderItemInput = document.getElementById("orderItemInput");
- var ItemNameInput = document.getElementById("ItemNameInput")
- var orderPackageSelect = document.getElementById("orderPackageSelect");
- var resultDiv = document.getElementById("result");
- var priceDiv = document.getElementById("price");
- var orderItem = orderItemInput.value;
- var itemName = ItemNameInput.value;
- var orderPackage = orderPackageSelect.value;
- // Calculate the price based on the selected package and distance
- var price = calculatePrice(orderPackage, distance);
- // Add the order to the orderList array
- orderList.push({
- orderItem: orderItem,
- orderPackage: orderPackage,
- price: price
- });
- // Add the ItemName to the NameList array
- itemNameList.push({
- itemName : itemName
- })
- // Update the total price by adding the price of the current order
- totalPrice += price;
- // Display all orders in the resultDiv
- resultDiv.textContent = "";
- priceDiv.textContent = "";
- for (var i = 0; i < orderList.length; i++) {
- resultDiv.innerHTML += "Order " + (i + 1) + ": Item - " + orderList[i].orderItem + ", Package - " + orderList[i].orderPackage + "<br>";
- priceDiv.innerHTML += "Order " + (i + 1) + ": Price - Kes" + orderList[i].price.toFixed(2) + "<br>"+ "<input name='amount[]' type='text' value='"+orderList[i].price.toFixed(2)+"' />";
- }
- for (var j = 0; j < itemName.length; j++) {
- itemName.innerHTML += "Item " + (j + 1) + ": " + itemName[j] + "<br>";
- }
- // Display all ItemNames in the Namediv
- ItemNameInput.textContent = "";
- // Update the display of the total price
- document.getElementById("totalPrice").textContent = "Total Price: Kes" + totalPrice.toFixed(2);
- document.getElementById("orderAmountInput").value = totalPrice;
- // Submit the form
- // Clear the input and select fields
- orderItemInput.value = "";
- orderPackageSelect.value = "";
- itemName.value = "";
- }
- function removeOrder() {
- var resultDiv = document.getElementById("result");
- var priceDiv = document.getElementById("price");
- var itemNameDiv = document.getElementById("ItemNameInput");
- if (orderList.length > 0) {
- // Get the price of the last order
- var removedOrderPrice = orderList[orderList.length - 1].price;
- // Subtract the price of the removed order from the total price
- totalPrice -= removedOrderPrice;
- // Remove the last order from the orderList array
- orderList.pop();
- // Display the updated list of orders and prices
- resultDiv.textContent = "";
- priceDiv.textContent = "";
- itemNameDiv.textContent = "";
- for (var i = 0; i < orderList.length; i++) {
- resultDiv.innerHTML += "Order " + (i + 1) + ": Item - " + orderList[i].orderItem + ", Package - " + orderList[i].orderPackage + "<br>";
- priceDiv.innerHTML += "Order " + (i + 1) + ": Price - Kes" + orderList[i].price.toFixed(2) + "<br>";
- document.getElementById("orderAmountInput").value = orderList[i].price.toFixed(2);
- }
- for (var j = 0; j < itemName.length; j++) {
- itemNameDiv.innerHTML += "Item " + (j + 1) + ": " + itemName[j] + "<br>";
- }
- }
- // Update the display of the total price
- document.getElementById("totalPrice").textContent = "Total Price: Kes " + totalPrice.toFixed(2);
- }
- function calculateDistancePrice(distance) {
- const basePrice = 500; // Base price for the first 5 km
- const extraPricePerKm = 50; // Additional price per km beyond the first 10 km
- if (distance <= 5) {
- return basePrice;
- } else {
- const extraDistance = distance - 5;
- const extraPrice = extraDistance * extraPricePerKm;
- return basePrice + extraPrice;
- }
- }
- function calculatePrice(orderPackage, distance) {
- // Add your price calculation logic here based on the selected package and distance
- // This is just a placeholder implementation
- console.log("Order Package: ", orderPackage);
- console.log("Distance: ", distance);
- if (orderPackage === "package1") {
- return 100 + calculateDistancePrice(distance);
- } else if (orderPackage === "package2") {
- return 200 + calculateDistancePrice(distance);
- } else if (orderPackage === "package3") {
- return 300 + calculateDistancePrice(distance);
- } else {
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement