Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.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 Google Maps API上的jQuery get函数失败_Javascript_Jquery_Google Maps_Get - Fatal编程技术网

Javascript Google Maps API上的jQuery get函数失败

Javascript Google Maps API上的jQuery get函数失败,javascript,jquery,google-maps,get,Javascript,Jquery,Google Maps,Get,我正在尝试使用GoogleMapsAPI构建一个小工具。我认为通过使用distanceMartrix api,我实际上只需要html/javascript就可以做到这一点 $.get("http://maps.googleapis.com/maps/api/distancematrix/json?origins=Vancouver+BC&destinations=San+Francisco|Victoria+BC&sensor=false", function(data){

我正在尝试使用GoogleMapsAPI构建一个小工具。我认为通过使用distanceMartrix api,我实际上只需要html/javascript就可以做到这一点

$.get("http://maps.googleapis.com/maps/api/distancematrix/json?origins=Vancouver+BC&destinations=San+Francisco|Victoria+BC&sensor=false", function(data){
    alert("Hello world");
});
上述代码在警报中失败。我可以让它在函数之前发出警报,在此之前我有jquery命令,它们都执行得很好。那么,这个极其简单的陈述出了什么问题?(我猜这很愚蠢,因为我累了!!)

因为您是从不同于脚本的域发出“AJAX”请求,所以它需要是一个JSONP请求,而不仅仅是一个JSON请求。JSONP需要在脚本中使用回调函数来接收数据并对其进行处理。如果您只是将
&callback=?
附加到URL的末尾,许多API将提供JSONP请求。jQuery将处理其余部分,使其进入成功回调


我猜就是这样。

看起来您需要通过标记
脚本
包含google maps v3 js库,然后使用此服务

会有这样的事情发生

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>


<script type=text/javascript">
    var service = new google.maps.DistanceMatrixService();
    service.getDistanceMatrix(

          {
            origins: [...], //array of origins
            destinations: [...], //aray of destionations
            travelMode: google.maps.TravelMode.DRIVING,
            unitSystem: google.maps.UnitSystem.METRIC,
            avoidHighways: false,
            avoidTolls: false
          }, function(response, status){
              if(status==google.maps.DistanceMatrixStatus.OK)
              {
                  alert('ok')
              }
          });
      }
</script>


谷歌地图API需要基本的身份验证(你必须为每天超过25k的请求付费)。加载他们的库会自动完成,但通过谷歌加载就可以了

为了进一步解释这个问题,您不能这样做,因为同一原产地政策。请参阅下面的第二个答案,因为它描述了如何正确使用来自Google的客户端API。