谷歌地图v3 API错误

6

我正在一个网站上使用Google Maps v3 API,一切都很正常。然而,自从昨天起,每次我尝试访问地图页面时,都会出现以下错误:

Error: Script terminated by timeout at: 
f@http://maps.googleapis.com/maps-api-v3/api/js/32/1a/map.js:1:175 
next@http://maps.googleapis.com/maps-api-v3/api/js/32/1a/map.js:2:310 
hx@http://maps.googleapis.com/maps-api-v3/api/js/32/1a/map.js:7:457
_.jl.prototype.Yb<@http://maps.googleapis.com/maps-api-v3/api/js/3/1a/map.js:54:163 
Uy@http://maps.googleapis.com/maps-api-v3/api/js/32/1a/map.js:38:313
Xy/<@http://maps.googleapis.com/maps-api-v3/api/js/32/1a/map.js:39:307 

有什么想法,为什么会发生这种情况?

我们也遇到了同样的问题。我已经阅读了所有答案,看起来我们都面临着同样的问题。事实是,在某些情况下,我们在 lat 和 lng 中发送 null(由于内部问题),而 Maps API 正常工作,但最近不再接受 null 了,所以导致程序被冻结并无限等待响应。这也可能是您的情况吗? - Manuel Alvarez
4个回答

3
这是有关经纬度的问题。经度和纬度应该是“float”类型的数字。如果我们传递其他数据类型,它会等待谷歌响应错误的数据类型,从而冻结浏览器。
function drawMap( lat, lng ) { lat = parseFloat(lat); lng = parseFloat(lng); if ( ! isNaN( lat ) && ! isNaN( lng ) ) // Before sending it to google let check if they are valid values
{
    var mapOptions = {
        center: new google.maps.LatLng(44.5452, -78.5389),
        zoom: 5
    };
    map = new google.maps.Map(document.getElementById('vehicle_country_region_from_map'),   mapOptions);
}}

3

你的代码是什么样子的? 今天,我发现自己也遇到了同样的问题(它在过去几个月一直在运行),来这里寻找答案,但最终还是自己找到了解决方案。

旧代码:

<script>
    function initMap(lat,lon) {
        var uluru = {lat: parseFloat(lat), lng: parseFloat(lon)};
        var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 4,
        center: uluru
    });
  }

Changed to:

<script>
    function initMap(lat,lon) {
        var uluru = {lat: lat, lng: lon};
        var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 4,
        center: uluru
    });
  }

请注意,在更改后,我将我的经度和纬度字符串解析为浮点数,然后才调用initMap函数。

这有助于找到我的问题。但我认为你把答案搞反了? - Dan Sewell

0

我曾经遇到同样的问题并寻找答案,但没有找到。最终我想出了一个解决方法,这可能会帮助你暂时解决问题。

在我的代码之前是这样的

          script.src = "https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&callback=initialize";

修改为

    script.src = "https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&callback=initMap&key=YOUR_API_KEY";

您可以从https://developers.google.com/maps/documentation/javascript/get-api-key生成API密钥,并按照https://developers.google.com/maps/中的initMap()代码进行操作。

这解决了我的问题。


还在处理中。至于你的代码,我不太确定,但我认为传感器参数已不再需要。 - Designer
2
正确 - 传感器参数不需要。此外,您正在使用“实验性”版本的代码库 - 如果要使用固定版本,则应将v = 3.exp更改为一个设置值,例如v = 3.30 - 这将防止库“自行更改”。更多详细信息请参见:https://developers.google.com/maps/documentation/javascript/versions - matt1

0

好的,找到了我的问题: (这个问题导致浏览器页面冻结,并出现原始问题中的错误)

原始代码:

if($('#map_canvas').length) {

    //var lat = 53.8683;
    //var lng = 0.5;

    // instantiate geocoder
    geocoder = new google.maps.Geocoder();
    // create new latitude / longitude object
    var latlng = new google.maps.LatLng(lat,lng);

    // set map options
    var myOptions = {
        zoom: 5,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    // display map
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    // show the map div
    $('#map_canvas').show();
}

取消这些行的注释:

var lat = 53.8683;
var lng = 0.5;

然后它起作用了。奇怪的是,这些变量一直被注释掉,直到现在都很好地工作了几年。


我已经谷歌搜索了很多次,但仍然没有找到解决方案。这也导致浏览器冻结。我曾认为这是Firefox的问题,但在Chrome上也发生了同样的事情。我甚至创建了新的API,但仍然是一样的。 - Designer

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