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

时间:2012-09-27 06:11:09

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

您好我正在使用此代码在Google地图中使用get-directions。但是当我从其他机器运行它时,它显示错误无效的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>

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

6 个答案:

答案 0 :(得分:33)

  

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

您需要将API从V2更改为V3,因为Google Map版本3不需要API密钥

检查this ..

将您的script写为

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

答案 1 :(得分:18)

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

答案 2 :(得分:9)

source

  

6月 2016 谷歌宣布他们将停止支持无钥匙   用法,表示任何不包含API密钥或客户端的请求   ID。这将在 2018年6月11日生效,无密钥访问将   支持的时间越长

答案 3 :(得分:0)

例如,您仍然可以在Google地图共享按钮中使用免费的iframe enter image description here

答案 4 :(得分:0)

嘿,您可以使用此插件

<iframe width="100%" height="100%" class="absolute inset-0" frameborder="0" title="map" marginheight="0" marginwidth="0" scrolling="no" src="https://maps.google.com/maps?width=100%&amp;height=600&amp;hl=en&amp;q=%C4%B0ikaneir+(Mumma's%20Bakery)&amp;ie=UTF8&amp;t=&amp;z=14&amp;iwloc=B&amp;output=embed" style="filter: scale(100) contrast(1.2) opacity(0.4);"></iframe>

答案 5 :(得分:0)

这个简单的代码可以 100% 工作,您只需要更改 'lat'、'long' 以显示地址

<iframe src="http://maps.google.com/maps?q=25.3076008,51.4803216&z=16&output=embed" height="450" width="600"></iframe>