【问题标题】:Can you load Google Maps API v3 via Google AJAX API loader您可以通过 Google AJAX API 加载器加载 Google Maps API v3
【发布时间】:2011-07-14 20:16:34
【问题描述】:

前段时间我使用了加载谷歌地图API的常规方法是这样的:

<script type="text/javascript" src="http://maps.google.com/maps?file=api&v=2&key=abcdefg&sensor=true">

后来我切换到 Google AJAX API 来加载 Google Maps API。这是因为我网站上的几个“小部件”需要 Google Ajax API 加载器,所以我选择保持一致并使用 AJAX API 来加载 Google 地图:

<script type="text/javascript" src="http://www.google.com/jsapi?key=abcdef"></script>
<script type="text/javascript">
  google.load("maps", "2", {"other_params": "sensor=true"});
</script>

现在我终于决定使用 Google Maps API v3,this page 没有在可用版本列表中列出 API v3。 API v3 文档中的示例都没有显示 AJAX API 的使用。是否可以(并且支持)通过 AJAX API 加载器加载 Google Maps API v3?

【问题讨论】:

    标签: google-maps google-maps-api-3 google-maps-api-2 google-ajax-api


    【解决方案1】:

    它没有记录,但它有效。

    google.load("maps", "3", {other_params:'key=YOUR_API_KEY', callback: function(){
      var map; // initialize your map in here
    }});
    

    [编辑] 该文档现在需要使用作为“key”参数传递给加载程序的 API 密钥。我已删除 'sensor=false' 作为参数,因为它现在明确不需要,并且在提供时会引发警告。

    【讨论】:

    • “未记录”是否意味着它可能(不)按预期工作或将来可能(不)工作?
    • @Salman 好吧,原来是documented,只是隐藏在常见问题解答的深处,而不是文档中。
    • 对...看来v3 api还有很长的路要走。
    • 只是为了说明如果您在单独的函数中设置地图时调用的外观:google.load('maps', '3', {other_params:'sensor=true', "回调" : yourFunctionName});
    • 如何添加 API 密钥?我不断收到关于萤火虫的警告Google Maps API warning: NoApiKeys https://developers.google.com/maps/documentation/javascript/error-messages#no-api-keys util.js (line 210)
    【解决方案2】:

    这个例子完美运行。

    //Set google Api
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    //Initialize General
    <script type="text/javascript">
        //Add Jquery, if you need
        google.load("jquery", "1.7.1");
        //Initialize Map, with option sensor=false
        google.load("maps", 3, {other_params:"key=YOUR_API_KEY"});
        //Initialize Map's
        google.setOnLoadCallback(function() {
                var options = {
                      zoom: 10,
                      center: new google.maps.LatLng(-34.616507,-58.409463),
                      mapTypeId: google.maps.MapTypeId.ROADMAP
                }
                map = new google.maps.Map(document.getElementById('map_canvas'), options);
        });
    
    
    </script>
    

    【讨论】:

    • 我已经用当前工作代码更新了这个答案。喜欢 Google 在没有警告的情况下更改内容。
    猜你喜欢
    • 2012-02-05
    • 1970-01-01
    • 1970-01-01
    • 2011-07-25
    • 2015-01-28
    • 1970-01-01
    • 1970-01-01
    • 2014-10-20
    • 1970-01-01
    相关资源
    最近更新 更多