Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么不是';t我的回调函数在使用Google距离矩阵服务API时被调用_Javascript_Jquery_Google Maps_Google Distancematrix Api - Fatal编程技术网

Javascript 为什么不是';t我的回调函数在使用Google距离矩阵服务API时被调用

Javascript 为什么不是';t我的回调函数在使用Google距离矩阵服务API时被调用,javascript,jquery,google-maps,google-distancematrix-api,Javascript,Jquery,Google Maps,Google Distancematrix Api,以下代码工作正常,但未调用回调函数。回调功能中没有任何警报显示,但同时显示开始和结束警报。我已经检查了控制台,Google返回了您期望的数据,示例如下 我关心的是, 谷歌回复(/**/\uxdc.\u6ct5vb&&uxdc.\u6ct5vb()之前的奇怪代码是什么 当包含Google javascript文件时,URL参数callback=initMap在做什么 谷歌回应: /**/_xdc_._6ct5vb && _xdc_._6ct5vb( { "destinat

以下代码工作正常,但未调用
回调
函数。
回调
功能中没有任何警报显示,但同时显示
开始
结束
警报。我已经检查了控制台,Google返回了您期望的数据,示例如下

我关心的是,

  • 谷歌回复(
    /**/\uxdc.\u6ct5vb&&uxdc.\u6ct5vb(
    )之前的奇怪代码是什么

  • 当包含Google javascript文件时,URL参数callback=initMap在做什么

谷歌回应:

/**/_xdc_._6ct5vb && _xdc_._6ct5vb( {
   "destination_addresses" : [ "Church Rd, Liverpool L36 9TJ, UK" ],
   "origin_addresses" : [ "Ashfield Rd, Liverpool L17 0BZ, UK" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "5.1 mi",
                  "value" : 8175
               },
               "duration" : {
                  "text" : "16 mins",
                  "value" : 959
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}
 )
Javascript/jQuery:

<script async defer src="https://maps.googleapis.com/maps/api/js?key=<?php echo GOOGLE_MAPS_API_KEY; ?>&callback=initMap" type="text/javascript"></script><!-- Google Maps -->

  <script>
$( document ).ready(function() {

// validate form
$( "#add_employee_mileage" ).validate({
errorClass: "error-class"
});

$( "#calculateMileage" ).click(function() {

alert('Start');

var origin = $( "#employee_mileage_start_postcode" ).val();
var destination = $( "#employee_mileage_end_postcode" ).val();

var service = new google.maps.DistanceMatrixService();

service.getDistanceMatrix(
    {
        origins: [origin],
        destinations: [destination],
        travelMode: google.maps.TravelMode.DRIVING,
        unitSystem: google.maps.UnitSystem.IMPERIAL,
        avoidHighways: false,
        avoidTolls: false
    }, 
    callback
);

function callback(response, status) {

    alert('Callback');

    if(status=="OK") {
        alert('Ok');
        alert(response.rows[0].elements[0].distance.value);
        $( "#employee_mileage_mileage" ).val(response.rows[0].elements[0].distance.value);
    } else {
        alert("Error: " + status);
    }

}

alert('End');

});

});
</script>

以下代码适用于我。请注意,没有
回调
函数

  <script>
$( document ).ready(function() {

// validate form
$( "#add_employee_mileage" ).validate({
errorClass: "error-class"
});

$( "#calculateMileage" ).click(function() {

var origin_postcode = $( "#employee_mileage_start_postcode" ).val();
var destination_postcode = $( "#employee_mileage_end_postcode" ).val();

var distanceService = new google.maps.DistanceMatrixService();
    distanceService.getDistanceMatrix({
        origins: [origin_postcode],
        destinations: [destination_postcode],
        travelMode: google.maps.TravelMode.DRIVING,
        unitSystem: google.maps.UnitSystem.IMPERIAL,
        avoidHighways: false,
        avoidTolls: false
    },
    function (response, status) {
        if (status !== google.maps.DistanceMatrixStatus.OK) {
            alert('Error: ', status);
        } else {
            $( "#employee_mileage_mileage" ).val((response.rows[0].elements[0].distance.value * 0.000621371).toFixed(2));
        }
    });

 });

});
</script>

$(文档).ready(函数(){
//验证表单
$(“添加员工里程”)。验证({
errorClass:“错误类”
});
$(“#calculateMileage”)。单击(函数(){
var origin_postcode=$(“#员工_里程_开始_postcode”).val();
var destination_postcode=$(“#员工_里程_end_postcode”).val();
var distanceService=new google.maps.DistanceMatrixService();
distanceService.getDistanceMatrix({
来源:[来源地\邮政编码],
目的地:[目的地\邮政编码],
travelMode:google.maps.travelMode.DRIVING,
unitSystem:google.maps.unitSystem.IMPERIAL,
避免:错误,
避免收费:错误
},
功能(响应、状态){
if(status!==google.maps.DistanceMatrixStatus.OK){
警报('错误:',状态);
}否则{
$(“#员工_里程_里程”).val((response.rows[0]。elements[0]。distance.value*0.000621371)。toFixed(2));
}
});
});
});

我没有地图。你为什么要添加地图?没有理由。你为什么不发布任何HTML?地图不应该是相关的。@geocodezip-对不起,这并不意味着你在没有地图的情况下就可以做小提琴了?
  <script>
$( document ).ready(function() {

// validate form
$( "#add_employee_mileage" ).validate({
errorClass: "error-class"
});

$( "#calculateMileage" ).click(function() {

var origin_postcode = $( "#employee_mileage_start_postcode" ).val();
var destination_postcode = $( "#employee_mileage_end_postcode" ).val();

var distanceService = new google.maps.DistanceMatrixService();
    distanceService.getDistanceMatrix({
        origins: [origin_postcode],
        destinations: [destination_postcode],
        travelMode: google.maps.TravelMode.DRIVING,
        unitSystem: google.maps.UnitSystem.IMPERIAL,
        avoidHighways: false,
        avoidTolls: false
    },
    function (response, status) {
        if (status !== google.maps.DistanceMatrixStatus.OK) {
            alert('Error: ', status);
        } else {
            $( "#employee_mileage_mileage" ).val((response.rows[0].elements[0].distance.value * 0.000621371).toFixed(2));
        }
    });

 });

});
</script>