Javascript 如何在没有 api 密钥的情况下使用谷歌地图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12615104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 10:50:32  来源:igfitidea点击:

How to use google maps without api key

javascriptgoogle-mapsgoogle-maps-api-3

提问by Mud Hut

Hi i am using this code to use get-directions in google maps. but when i run this from other machines it is showing the error invalid api key. how to use this same map without an api key.

嗨,我正在使用此代码在谷歌地图中使用获取方向。但是当我从其他机器上运行它时,它显示了错误无效的 api 密钥。如何在没有 api 密钥的情况下使用相同的地图。

<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=ABQIAAAAuPsJpk3MBtDpJ4G8cqBnjRRaGTYH6UMl8mADNa0YKuWNNa8VNxQCzVBXTx2DYyXGsTOxpWhvIG7Djw" type="text/javascript"></script>
    <style type="text/css">
    body {
      background:#FFE1C7;
      font-family: Verdana, Arial, sans serif;
      font-size: 11px;
      margin: 2px;
    }
table.directions th {
      background-color:#EEEEEE;
    }

    img {
      color: #000000;
    }
    </style>
    <script type="text/javascript">

    var map;
    var gdir;
    var geocoder = null;
    var addressMarker;

    function initialize() {
      if (GBrowserIsCompatible()) {      
        map = new GMap2(document.getElementById("map_canvas"));
        gdir = new GDirections(map, document.getElementById("directions"));
        GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);

        setDirections("Bangalore", "Mysore", "en_US");
      }
    }

    function setDirections(fromAddress, toAddress, locale) {
      gdir.load("from: " + fromAddress + " to: " + toAddress,
                { "locale": locale });
    }

    function handleErrors(){
     if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
       alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
     else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
       alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);

     else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
       alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);

    //   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
    //     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);

     else if (gdir.getStatus().code == G_GEO_BAD_KEY)
       alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

     else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
       alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);

     else alert("An unknown error occurred.");
    }

    function onGDirectionsLoad(){
        // Use this function to access information about the latest load()
        // results.

        // e.g.
        // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
      // and yada yada yada...
    }

    </script>

Can anyone help me...thanks in advance..

任何人都可以帮助我...提前致谢...

回答by Usman

Note : This answer is now out-of-date. You are now required to have an API key to use google maps. Read More

注意:此答案现已过时。您现在需要有一个 API 密钥才能使用谷歌地图。阅读更多

you need to change your API from V2 to V3, Since Google Map Version 3 don't required API Key

您需要将 API 从 V2 更改为 V3,因为 Google Map Version 3 不需要 API Key

Check thisout..

看看这个..

write your scriptas

把你script写成

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

回答by Brane

Now you must have API key. You can generate that in google developer console. Here is LINKto the explanation.

现在您必须拥有 API 密钥。您可以在 google 开发者控制台中生成它。这是解释的链接

回答by Ted

source

来源

In June 2016Google announced that they would stop supporting keyless usage, meaning any request that doesn't include an API key or Client ID. This will go into effect on June 11 2018, and keyless access will nolonger be supported

2016年 6月,Google 宣布他们将停止支持无密钥使用,这意味着任何不包含 API 密钥或客户端 ID 的请求。这将生效于2018年6月11日,和无钥匙进入将不会再支持