Javascript 打印和大量谷歌地图(api)

Javascript 打印和大量谷歌地图(api),javascript,php,jquery,google-maps,google-maps-api-3,Javascript,Php,Jquery,Google Maps,Google Maps Api 3,我想我在这里是在我的头,但我是如此接近完成,所以我希望你能帮我一把 我制作了一个web应用程序来处理活动邀请。它管理邀请函,将人们聚集在一起,并根据活动指示安排需要发送给每个参与者的所有信件 在最好的情况下,我希望这封信还包含一个谷歌地图,它将participent指向事件的正确地址。地址不是固定的,而是根据participent和时间而变化的 我用一个可打印的网页来创建这些信件,该网页可以生成大约180页。 谷歌地图正在运行,但在大约15-20页之后,指示就停止了,只显示了一张瑞典的图片 下面

我想我在这里是在我的头,但我是如此接近完成,所以我希望你能帮我一把

我制作了一个web应用程序来处理活动邀请。它管理邀请函,将人们聚集在一起,并根据活动指示安排需要发送给每个参与者的所有信件

在最好的情况下,我希望这封信还包含一个谷歌地图,它将participent指向事件的正确地址。地址不是固定的,而是根据participent和时间而变化的

我用一个可打印的网页来创建这些信件,该网页可以生成大约180页。 谷歌地图正在运行,但在大约15-20页之后,指示就停止了,只显示了一张瑞典的图片

下面的示例代码经过简化,但产生了相同的问题。我能做些什么来正确绘制180张地图? 加载整个页面需要1分钟对我来说并不重要

我用fiddle做了一些错误的事情,所以它不会在下面重复,但你明白了

$document.readyfunction{ $maps=$'.googleMap'; $maps.eachfunctionindex,元素{ $infotext=$Element.children.infotext'; var方向显示; var directionsService=新的google.maps.directionsService; var映射; directionsDisplay=新建google.maps.DirectionsRenderer; var begin=new google.maps.LatLng60.216667,16.333333; 变量映射选项={ 缩放:6, 中心:开始 } map=new google.maps.mapement,mapOptions; directionsDisplay.setMapmap; var start=$infotext.children.地址为“'u start'.text+”,“+$infotext.children.”城市“'u start'.text+”,“+$infotext.children'.zip\u start'.text+”,“+$infotext.children'.country\u start'.text; var end=$infotext.children'。address_end.text+','+$infotext.children.city_end.text+','+$infotext.children.zip_end.text+','+$infotext.children.country_end.text; var请求={ 来源:start, 目的地:完, 航路点:错误, travelMode:google.maps.travelMode.BICYCLING }; directionsService.RouterRequest、functionresponse、状态{ 如果status==google.maps.DirectionsStatus.OK{ directionsDisplay.setDirectionsresponse; var route=response.routes[0]; } }; }; }; 身体{ 字体系列:开放式Sans,无衬线; 字体大小:13px; 线高:1.62em; } .第页{ 宽度:210毫米; 最小高度:297mm; 填料:20mm; 边缘:10mm自动; 边框:1pxD33实心; 边界半径:5px; 背景:白色; 长方体阴影:0.5pxRGBA0,0,0,0.1; } .页眉{ 边缘底部:150px; } .pageMark{ 浮动:对; 字号:0.8em; } @页面{ 尺寸:A4; 保证金:0; } @媒体印刷品{ html, 身体{ 宽度:210毫米; 高度:297mm; } .第页{ 保证金:0; 边界:首字母; 边界半径:初始; 宽度:首字母; 最小高度:初始高度; 盒影:首字母; 背景:初始; 分页符后:始终; } } 斯德哥尔摩 斯维里奇 哥德堡 斯维里奇
谷歌不喜欢你在短时间内多次查询他们的服务器。他们的速率限制为每秒不超过10个请求

使用代码,您可以在几乎相同的时间获取每个地图的方向

试试这个,至少对我有用

我会将我的查询限制为每1到5秒查询1次。在我的示例中,我在两个请求之间等待2秒钟

以下是您的更新代码:

$(document).ready(function() {

    // Set the time to wait between requests
    var increment = 2000;

    // Added this line to keep track of the timeout
    var timeout = 0;

    $maps = $('.googleMap');
    $maps.each(function (index, Element) {

        // Add more timeout
        timeout += increment;

        // Set a timeout before we initialize each map
        setTimeout(function () {

            $infotext = $(Element).children('.infotext');
            var directionsDisplay;
            var directionsService = new google.maps.DirectionsService();
            var map;

            directionsDisplay = new google.maps.DirectionsRenderer();

            var begin = new google.maps.LatLng(60.216667, 16.333333);
            var mapOptions = {
                zoom: 6,
                center: begin
            }

            map = new google.maps.Map(Element, mapOptions);
            directionsDisplay.setMap(map);

            var start = $infotext.children('.address_start').text() + ', ' + $infotext.children('.city_start').text() + ', ' + $infotext.children('.zip_start').text() + ', ' + $infotext.children('.country_start').text();
            var end = $infotext.children('.address_end').text() + ', ' + $infotext.children('.city_end').text() + ', ' + $infotext.children('.zip_end').text() + ', ' + $infotext.children('.country_end').text();

            var request = {
                origin: start,
                destination: end,
                optimizeWaypoints: false,
                travelMode: google.maps.TravelMode.BICYCLING
            };

            directionsService.route(request, function (response, status) {
                if (status == google.maps.DirectionsStatus.OK) {
                    directionsDisplay.setDirections(response);
                    var route = response.routes[0];
                }
            });
        }, timeout);
    });

});