• <small id='otkhu'></small><noframes id='otkhu'>

      <legend id='otkhu'><style id='otkhu'><dir id='otkhu'><q id='otkhu'></q></dir></style></legend>
    1. <i id='otkhu'><tr id='otkhu'><dt id='otkhu'><q id='otkhu'><span id='otkhu'><b id='otkhu'><form id='otkhu'><ins id='otkhu'></ins><ul id='otkhu'></ul><sub id='otkhu'></sub></form><legend id='otkhu'></legend><bdo id='otkhu'><pre id='otkhu'><center id='otkhu'></center></pre></bdo></b><th id='otkhu'></th></span></q></dt></tr></i><div id='otkhu'><tfoot id='otkhu'></tfoot><dl id='otkhu'><fieldset id='otkhu'></fieldset></dl></div>
        <bdo id='otkhu'></bdo><ul id='otkhu'></ul>

    2. <tfoot id='otkhu'></tfoot>

        谷歌地图 API;“CORS 标头‘Access-Control-Allow-Origin’缺失"错误

        Google Maps API; quot;CORS header ‘Access-Control-Allow-Origin’ missingquot; error(谷歌地图 API;“CORS 标头‘Access-Control-Allow-Origin’缺失错误)

          <tbody id='2KiwX'></tbody>
        <tfoot id='2KiwX'></tfoot>
      1. <i id='2KiwX'><tr id='2KiwX'><dt id='2KiwX'><q id='2KiwX'><span id='2KiwX'><b id='2KiwX'><form id='2KiwX'><ins id='2KiwX'></ins><ul id='2KiwX'></ul><sub id='2KiwX'></sub></form><legend id='2KiwX'></legend><bdo id='2KiwX'><pre id='2KiwX'><center id='2KiwX'></center></pre></bdo></b><th id='2KiwX'></th></span></q></dt></tr></i><div id='2KiwX'><tfoot id='2KiwX'></tfoot><dl id='2KiwX'><fieldset id='2KiwX'></fieldset></dl></div>
              <bdo id='2KiwX'></bdo><ul id='2KiwX'></ul>

                <small id='2KiwX'></small><noframes id='2KiwX'>

                1. <legend id='2KiwX'><style id='2KiwX'><dir id='2KiwX'><q id='2KiwX'></q></dir></style></legend>

                  本文介绍了谷歌地图 API;“CORS 标头‘Access-Control-Allow-Origin’缺失"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试使用 Google Maps API 计算两个地方之间的预计旅行时间.我通过以下方式要求数据:

                  I'm trying to calculate the estimated travel time between two places, with the Google Maps API. I am asking for the data in the following way:

                  const Url = 'https://maps.googleapis.com/maps/api/distancematrix/json?origins=25.7694708,-80.259947&destinations=25.768915,-80.254659&key=' + API_KEY;
                  try {
                    const response = await fetch(Url);
                    console.log(response);
                    const data = await response.json();
                    console.log(data.rows);
                  } catch(e){
                    console.log(e);
                  }
                  

                  问题是在浏览器控制台中出现错误:

                  The problem is that in the browser console I get the error:

                  TypeError: NetworkError when attempting to fetch resource
                  

                  它还向我显示了一个警告:

                  and it also shows me a warning:

                  Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://maps.googleapis.com/maps/api/distancematrix/json?origins=25.7694708,-80.259947&destinations=25.768915,-80.254659&key=API_KEY. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
                  

                  但是当我查看网络部分的控制台时,它显示调用已成功完成,并显示以下 json:

                  But when I look at the console in the network part it shows me that the call was completed successfully and it shows me the following json:

                  {
                     "destination_addresses" : [ "3670 SW 3rd St, Miami, FL 33135, USA" ],
                     "origin_addresses" : [ "3911 SW 2nd Terrace, Coral Gables, FL 33134, USA" ],
                     "rows" : [
                        {
                           "elements" : [
                              {
                                 "distance" : {
                                    "text" : "0.9 km",
                                    "value" : 881
                                 },
                                 "duration" : {
                                    "text" : "2 mins",
                                    "value" : 144
                                 },
                                 "status" : "OK"
                              }
                           ]
                        }
                     ],
                     "status" : "OK"
                  }
                  

                  有人可以帮我解决这个问题吗?我在网站上尝试过类似的问题,但我无法解决问题.提前致谢.

                  Can someone help me solve this problem? I have tried similar questions on the site but I have not been able to solve the problem. Thanks in advance.

                  推荐答案

                  您正在客户端使用距离矩阵服务.但是客户端(浏览器)站点不支持您尝试访问 API 的方式,并且它不会可靠地工作.

                  You are using Distance Matrix Service on client side. But the manner you trying to access the API is not supported for client(browser)-site and it will not work reliably.

                  很高兴有一个适用于您的用例的库:这是客户端距离矩阵服务的指南.这是一个 vanilla-js 示例 看看.

                  Good that there is a library for your use-case: Here's the guide for the Distance Matrix Service on client side. And here's a vanilla-js example Check it out.

                  也许这个片段会对你有所帮助:

                  Maybe this snippet will help you:

                  const matrix = new google.maps.DistanceMatrixService();
                  
                  matrix.getDistanceMatrix({
                    origins: [new google.maps.LatLng(25.7694708, -80.259947)],
                    destinations: [new google.maps.LatLng(25.768915, -80.254659)],
                    travelMode: google.maps.TravelMode.DRIVING,
                  }, function(response, status) {
                    //do something
                  });
                  

                  这篇关于谷歌地图 API;“CORS 标头‘Access-Control-Allow-Origin’缺失"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  业务场景:使用update语句去更新数据库字段。 原因:update接收值不正确。原来代码: $query = "UPDATE student SET date = now() WHERE id = $id";$result = $mysqli-query($query2) or die($mysqli-error); // 问题出现了在这句 $data = $result-fetch_ass
                  在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
                  问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)

                    <tbody id='kB4de'></tbody>

                  <small id='kB4de'></small><noframes id='kB4de'>

                  <legend id='kB4de'><style id='kB4de'><dir id='kB4de'><q id='kB4de'></q></dir></style></legend>

                    <bdo id='kB4de'></bdo><ul id='kB4de'></ul>

                          • <tfoot id='kB4de'></tfoot>

                            <i id='kB4de'><tr id='kB4de'><dt id='kB4de'><q id='kB4de'><span id='kB4de'><b id='kB4de'><form id='kB4de'><ins id='kB4de'></ins><ul id='kB4de'></ul><sub id='kB4de'></sub></form><legend id='kB4de'></legend><bdo id='kB4de'><pre id='kB4de'><center id='kB4de'></center></pre></bdo></b><th id='kB4de'></th></span></q></dt></tr></i><div id='kB4de'><tfoot id='kB4de'></tfoot><dl id='kB4de'><fieldset id='kB4de'></fieldset></dl></div>