提问者:小点点

Google maps v3 API仅在刷新/身份验证后加载速度非常慢


使用谷歌地图v3api。当我使用地图访问页面时,有时会出现以下错误:“custom.js:46 uncaughtreferenceerror:google未定义”

已在密钥上启用API:

  • 谷歌地图方向API
  • 谷歌地图距离矩阵API
  • 谷歌地图高程API
  • 谷歌地图地理编码API
  • 谷歌地图JavaScript API

当我重新加载页面时,一切正常。这不是100%有效的。在某些情况下需要多次重新加载。

我确实注意到,当地图加载不正确时,此脚本将在我的头标记中运行:

<script type="text/javascript" charset="UTF-8" src="https://maps.googleapis.com/maps/api/js/AuthenticationService.Authenticate?1shttp%3A%2F%2Fmayan-co.com%2Fen%2Foutlets&amp;MYKEY&amp;callback=_xdc_._h7cv8d&amp;token=60166"></script>

10-20秒后,这个脚本消失了,当我刷新这个脚本消失后的页面时,我的地图工作正常。

我尝试过但没有结果的事情:

  • 将加载API的脚本放在页脚中。
  • 不使用异步或延迟。
  • 向我的api密钥添加特定的url。
  • 不使用api密钥

将api脚本加载到我的页面头部:

<script src="https://maps.googleapis.com/maps/api/js?key=MYKEY" async defer></script>

渲染地图并在地图上放置标记的脚本(加载到页脚)

jQuery(document).ready(function($) {
    $('#animation').addClass('animate');

    $('.heart img').popover({
        placement: 'top',
        html: true,
        container: '#animation',
        content: function () {
            return $(this).attr('alt');
        }
    });

    $('body').on('click', function(e) {
        $('.heart img').each(function() {
            if (!$(this).is(e.target) && $(this).has(e.target).length === 0 && $('.heart img').has(e.target).length === 0) {
                $(this).popover('hide');
            } else {
                $(this).popover('toggle');
            }
        });
    });

    function render_map($el) {

        var $markers = $(document).find('#locations .data-source li');

        var args = {
            zoom: 16,
            center: new google.maps.LatLng(0, 0),
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            scrollwheel: false,
            mapTypeControlOptions: {
                mapTypeIds: [google.maps.MapTypeId.ROADMAP]
            }
        };

        var map = new google.maps.Map($el[0], args);

        map.markers = [];

        index = 0;
        $markers.each(function() {
            add_marker($(this), map, index);
            index++;
        });

        center_map(map);
    }

    function add_marker($marker, map, index) {
        var latlng = new google.maps.LatLng($marker.attr('data-lat'), $marker.attr('data-lng'));
        var image = '../../img/maps-leaf.png';
        var marker = new google.maps.Marker({
            position: latlng,
            map: map,
            icon: image
        });
        map.markers.push(marker);
        if ($marker.html()) {
            $('#locations .data-display').append('<li class="linkage" id="p'+index+'">'+$marker.html()+'</li>');

            $(document).on('click', '#p' + index, function() {
                infowindow.open(map, marker);
                setTimeout(function() { infowindow.close(); }, 5000);
            });

            var infowindow = new google.maps.InfoWindow({
                content: $marker.html(),
            });

            google.maps.event.addListener(marker, 'click', function() {
                infowindow.open( map, marker );
            });
        }
    }

    function center_map(map) {
        var bounds = new google.maps.LatLngBounds();
        $.each( map.markers, function( i, marker ){
            var latlng = new google.maps.LatLng( marker.position.lat(), marker.position.lng() );
            bounds.extend( latlng );
        });

        if( map.markers.length == 1 ) {
            map.setCenter( bounds.getCenter() );
            map.setZoom( 16 );
        } else {
            map.fitBounds( bounds );
        }
    }

    $(document).ready(function(){
        $('#map').each(function(){
            render_map( $(this) );
        });
    });
});

共1个答案

匿名用户

您描述的内容看起来像是您试图在加载谷歌地图应用编程接口JavaScript之前实例化地图(您在那里使用了async属性)。这就是为什么它有时会抛出谷歌未定义错误。

换句话说,文档。在https://maps.googleapis.com/maps/api/js?key=MYKEY已加载。

Google Maps API允许您在URL中使用回调参数以及API完全加载后调用的函数名。看见https://developers.google.com/maps/documentation/javascript/tutorial.

因此,在您的情况下,您将使用如下URL:

https://maps.googleapis.com/maps/api/js?key=MYKEY&callback=initMap

然后初始化映射:

initMap() {
    var map = new google.maps.Map($el[0], args);
}