Can you load Google Maps API v3 via Google AJAX API loader

烈酒焚心 提交于 2019-12-20 10:11:17

问题


Some time ago I used the regular method of loading Google Maps API like this:

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

Later I switched to Google AJAX APIs to load Google Maps API. This was because a couple of "widgets" on my website needed the Google Ajax API loader so I chose to be consistent and used the AJAX APIs to load Google Maps as well:

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

Now that I have finally decided to use Google Maps API v3, this page does not list API v3 in the available version list. None of the examples on API v3 documentation show the use of AJAX APIs as well. Is is possible (and supported) to load Google Maps API v3 via AJAX API loader?


回答1:


It's undocumented, but it works.

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

[EDIT] The documentation now requires the use of an API key that is passed to the loader as a "key" parameter. I've removed 'sensor=false' as a parameter because it is now explicitly not required and throws a warning when provided.




回答2:


this example work perfectly.

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


来源:https://stackoverflow.com/questions/5296115/can-you-load-google-maps-api-v3-via-google-ajax-api-loader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!