你能否通过Google AJAX API加载Google Maps API v3?

7

一段时间以前,我使用了常规的加载Google Maps API的方法,如下所示:

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

后来我改用Google AJAX APIs来加载Google Maps API。这是因为我的网站上有几个“小部件”需要Google Ajax API加载程序,所以我选择保持一致并使用AJAX APIs来加载Google Maps:

<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,但是这个页面上没有列出API v3的版本。在API v3文档中的示例中也没有展示如何使用AJAX API。那么是否可以通过AJAX API加载Google Maps API v3呢?这种方式是否被支持呢?

2个回答

24

这并没有被记录在文档中,但它可以正常工作。

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

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


1
“未记录文档”是指它可能(不)按预期工作,或者将来可能(不)按预期工作? - Salman A
3
好的,原文是:“@Salman Well, turns out it's documented, just hidden deep inside the FAQ, rather than the documentation.” 我的翻译是:“嗯,事实证明它已经被记录下来了,只是被放在常见问题解答中的深处,而不是文档中。” - HChen
好吧...看起来v3的API仍有很长的路要走。 - Salman A
1
只是为了澄清,如果您在单独的函数中设置地图,调用将如何呈现:google.load('maps', '3', {other_params:'sensor=true', "callback" : yourFunctionName}); - MattC
2
你如何添加API密钥?我在Firebug上一直收到这个警告:“Google Maps API warning: NoApiKeys https://developers.google.com/maps/documentation/javascript/error-messages#no-api-keys util.js (line 210)” - user2636556
显示剩余2条评论

-1

这个例子运行得非常完美。

//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>

我已经更新了这个答案,附上目前可用的代码。当谷歌没有警告地更改东西时,真是太棒了。 - Nilpo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接