{"id":4431,"date":"2024-09-06T11:14:00","date_gmt":"2024-09-06T11:14:00","guid":{"rendered":"https:\/\/portfolio.zenkoders.com\/car\/?post_type=product&#038;p=4431"},"modified":"2024-09-06T16:44:16","modified_gmt":"2024-09-06T16:44:16","slug":"towing-services","status":"publish","type":"product","link":"https:\/\/portfolio.zenkoders.com\/car\/product\/towing-services\/","title":{"rendered":"Towing services"},"content":{"rendered":"\n    <!-- Include Leaflet CSS and JS -->\n    <link rel=\"stylesheet\" href=\"https:\/\/unpkg.com\/leaflet@1.9.3\/dist\/leaflet.css\" \/>\n    <script src=\"https:\/\/unpkg.com\/leaflet@1.9.3\/dist\/leaflet.js\"><\/script>\n\n    <!-- Include Leaflet Control Geocoder for Autocomplete -->\n    <script src=\"https:\/\/unpkg.com\/leaflet-control-geocoder\/dist\/Control.Geocoder.js\"><\/script>\n    <script src=\"https:\/\/portfolio.zenkoders.com\/car\/wp-includes\/js\/jquery\/jquery.js\"><\/script> <!-- Ensure jQuery is included -->\n\n    <form id=\"distance-calculator-form\">\n        <label for=\"current-location\">Current Location:<\/label>\n        <input type=\"text\" id=\"current-location\" name=\"current_location\" required placeholder=\"Enter current address\">\n\n        <label for=\"destination\">Destination:<\/label>\n        <input type=\"text\" id=\"destination\" name=\"destination\" required placeholder=\"Enter destination address\">\n\n        <button type=\"button\" id=\"calculate-price\">Calculate Price<\/button>\n\n        <div id=\"price-result\"><\/div>\n    <\/form>\n\n    <script>\n    \/\/ Initialize the Leaflet Geocoder for address autocomplete\n    var geocoder = L.Control.Geocoder.nominatim();\n\n    function geocodeAddress(address, callback) {\n        var url = `https:\/\/nominatim.openstreetmap.org\/search?format=json&q=${encodeURIComponent(address)}&limit=1`;\n\n        fetch(url, { method: 'GET', headers: { 'Content-Type': 'application\/json' }})\n            .then(response => response.json())\n            .then(data => {\n                if (data && data.length > 0) {\n                    var lat = parseFloat(data[0].lat);\n                    var lon = parseFloat(data[0].lon);\n                    callback(null, { lat, lon });\n                } else {\n                    callback('Location not found. Please enter a valid address.');\n                }\n            })\n            .catch(error => callback('Error fetching location data: ' + error.message));\n    }\n\n    document.getElementById('calculate-price').addEventListener('click', function() {\n        var currentLocation = document.getElementById('current-location').value;\n        var destination = document.getElementById('destination').value;\n\n        if (currentLocation && destination) {\n            geocodeAddress(currentLocation, function(err, currentLocationCoords) {\n                if (err) {\n                    document.getElementById('price-result').textContent = err;\n                    return;\n                }\n\n                geocodeAddress(destination, function(err, destinationCoords) {\n                    if (err) {\n                        document.getElementById('price-result').textContent = err;\n                        return;\n                    }\n\n                    \/\/ Calculate the distance between two coordinates\n                    var distance = calculateDistance(currentLocationCoords.lat, currentLocationCoords.lon, destinationCoords.lat, destinationCoords.lon);\n\n                    \/\/ Example price calculation based on distance\n                    var price = distance * 0.5; \/\/ Example pricing model: $0.50 per km\n\n                    var priceResultDiv = document.getElementById('price-result');\n                    priceResultDiv.innerHTML = 'Estimated Price: $' + price.toFixed(2) + '<br\/><button type=\"button\" id=\"book-service\">Book Your Service<\/button>';\n\n                    document.getElementById('book-service').addEventListener('click', function() {\n                        \/\/ Send AJAX request to add product to cart\n                        jQuery.post('https:\/\/portfolio.zenkoders.com\/car\/wp-admin\/admin-ajax.php', {\n                            action: 'add_to_cart',\n                            product_id: 4431,\n                            price: price\n                        }, function(response) {\n                            if (response.success) {\n                                alert('Product added to cart successfully!');\n                                location.reload(); \/\/ Reload to update cart display\n                            } else {\n                                alert('Failed to add product to cart.');\n                            }\n                        });\n                    });\n                });\n            });\n        } else {\n            document.getElementById('price-result').textContent = 'Please enter both locations.';\n        }\n    });\n\n    \/\/ Function to calculate distance between two coordinates using Haversine formula\n    function calculateDistance(lat1, lon1, lat2, lon2) {\n        const R = 6371; \/\/ Radius of the Earth in km\n        const dLat = toRad(lat2 - lat1);\n        const dLon = toRad(lon2 - lon1);\n        const a = Math.sin(dLat \/ 2) * Math.sin(dLat \/ 2) +\n                  Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) *\n                  Math.sin(dLon \/ 2) * Math.sin(dLon \/ 2);\n        const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));\n        return R * c; \/\/ Distance in km\n    }\n\n    function toRad(degrees) {\n        return degrees * (Math.PI \/ 180);\n    }\n    <\/script>\n\n    \n","protected":false},"excerpt":{"rendered":"","protected":false},"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","meta":[],"product_brand":[],"product_cat":[15],"product_tag":[],"class_list":["post-4431","product","type-product","status-publish","product_cat-uncategorized","tm-no-options","first","instock","shipping-taxable","product-type-simple"],"_links":{"self":[{"href":"https:\/\/portfolio.zenkoders.com\/car\/wp-json\/wp\/v2\/product\/4431","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/portfolio.zenkoders.com\/car\/wp-json\/wp\/v2\/product"}],"about":[{"href":"https:\/\/portfolio.zenkoders.com\/car\/wp-json\/wp\/v2\/types\/product"}],"replies":[{"embeddable":true,"href":"https:\/\/portfolio.zenkoders.com\/car\/wp-json\/wp\/v2\/comments?post=4431"}],"wp:attachment":[{"href":"https:\/\/portfolio.zenkoders.com\/car\/wp-json\/wp\/v2\/media?parent=4431"}],"wp:term":[{"taxonomy":"product_brand","embeddable":true,"href":"https:\/\/portfolio.zenkoders.com\/car\/wp-json\/wp\/v2\/product_brand?post=4431"},{"taxonomy":"product_cat","embeddable":true,"href":"https:\/\/portfolio.zenkoders.com\/car\/wp-json\/wp\/v2\/product_cat?post=4431"},{"taxonomy":"product_tag","embeddable":true,"href":"https:\/\/portfolio.zenkoders.com\/car\/wp-json\/wp\/v2\/product_tag?post=4431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}