如何在PHP中保存Distance Matrix API的距离结果以使用公式计算票价

时间:2019-07-19 07:47:25

标签: javascript php google-distancematrix-api

我使用PHP创建了一个表单,用于添加预订并在添加预订之前计算票价。我将Google Places API用于自动填充地址。当用户单击“计算票价”按钮时,它会使用JS在表格下方显示以英里,公里,总时间,分钟为单位的距离,并使用ID显示。

问题是如何将这些值保存在PHP变量中,因为我必须执行票价计算才能在DB中显示和保存这些值。

由于我的JS不好,而且找不到与我的问题相近的东西。这就是为什么我在这里写它。

计算结果代码

function calculateDistance() {
    var origin = $('#origin').val();
    var destination = $('#destination').val();
    var service = new google.maps.DistanceMatrixService();
    service.getDistanceMatrix(
        {
            origins: [origin],
            destinations: [destination],
            travelMode: google.maps.TravelMode.DRIVING,
            unitSystem: google.maps.UnitSystem.IMPERIAL, // miles and feet.
            // unitSystem: google.maps.UnitSystem.metric, // kilometers and meters.
            avoidHighways: false,
            avoidTolls: false
        }, callback);
}

获得计算结果

function callback(response, status) {
            if (status != google.maps.DistanceMatrixStatus.OK) {
                $('#result').html(err);
            } else {
                var origin = response.originAddresses[0];
                var destination = response.destinationAddresses[0];
                if (response.rows[0].elements[0].status === "ZERO_RESULTS") {
                    $('#result').html("Better get on a plane. There are no roads between "  + origin + " and " + destination);
                } 

                else {
                    var distance = response.rows[0].elements[0].distance;
                    var duration = response.rows[0].elements[0].duration;
                    console.log(response.rows[0].elements[0].distance);
                    var distance_in_kilo = distance.value / 1000; // the kilom
                    var distance_in_mile = distance.value / 1609.34; // the mile
                    var duration_text = duration.text;
                    var duration_value = duration.value;
                    $('#in_mile').text(distance_in_mile.toFixed(2));
                    $('#in_kilo').text(distance_in_kilo.toFixed(2));
                    $('#duration_text').text(duration_text);
                    $('#duration_value').text(duration_value);
                }
            }
        }

中显示结果
<div id="result">
<ul class="list-group">

<li class="list-group-item d-flex justify-content-between align-items-center">Distance in Miles<span  id="in_mile" class="badge badge-primary badge-pill"></span>
</li>

<li class="list-group-item d-flex justify-content-between align-items-center">Distance in Kilometers<span  id="in_kilo" class="badge badge-primary badge-pill"></span>
</li>

<li class="list-group-item d-flex justify-content-between align-items-center">Total Time<span  id="duration_text" class="badge badge-primary badge-pill"></span>
</li>

<li class="list-group-item d-flex justify-content-between align-items-center">In Seconds<span  id="duration_value" class="badge badge-primary badge-pill"></span>
</li>

图片 Screenshot for better understanding

0 个答案:

没有答案