如何获得距离"价值"来自谷歌地图距离矩阵api?

时间:2018-05-24 06:07:36

标签: javascript google-maps-api-3 google-distancematrix-api

我使用下面的自动填充输入字段,它给出了distance.text值,以km表示,但我想得到精度距离的distance.value。

function getdistance(address){
    var dest_address = address; // Address enter by user
    var origin_address = document.getElementById('hall').value ; // Address where you need to calculate distance.
    var distanceService = new google.maps.DistanceMatrixService(); // store Google Distance Service method

    // Call distance matrix services.
    // default parameters
    distanceService.getDistanceMatrix({
        origins: [dest_address],
        destinations: [origin_address],
        travelMode: google.maps.TravelMode.DRIVING,
        unitSystem: google.maps.UnitSystem.METRIC,
        durationInTraffic: true,
        avoidHighways: false,
        avoidTolls: false
        },
    function (response, status) {
        // check status from google service call
        if (status !== google.maps.DistanceMatrixStatus.OK) {
            console.log('Error:', status);
        } else {
            // Response contain JSON.
            var error = response.rows[0].elements[0].status;
            $('#error').html(error);
            var distance = response.rows[0].elements[0].distance.value;
            document.getElementById("distance").value = distance;

            }
    });

}

我尝试过更改

var distance = response.rows[0].elements[0].distance.text;

var distance = response.rows[0].elements[0].distance.value;

但仍然只获得"文字"值。 请帮忙

1 个答案:

答案 0 :(得分:0)

实际上,抱歉!将线路改为......

var distance = response.rows[0].elements[0].distance.value;

...作品。我只需要清除缓存中的.js文件更改就可以了。= / = / = /