【问题标题】:Google Maps API won't work on mobile browser [closed]Google Maps API 无法在移动浏览器上运行 [关闭]
【发布时间】:2020-07-03 02:44:05
【问题描述】:

我在我的网站上使用 Google Maps API 自定义标记,它在桌面浏览器中运行良好,但不会出现在移动浏览器中。我的控制台出现错误: TypeError: Cannot read property 'length' of undefined.

我使用 ajax 获取数据。这是javascript:

var myLatlng = new google.maps.LatLng(-0.7893, 113.9213);
var mapOptions = {
    zoom: 4,
    center: myLatlng,
    scrollwheel: false, 
    mapTypeControl: false,
    styles: []
};

var locations = response;

var map = new google.maps.Map(document.getElementById("map"), mapOptions);

var infowindow = new google.maps.InfoWindow();

var marker, i;

for (i = 0; i < locations.length; i++) {  
    marker = new google.maps.Marker({
    position: new google.maps.LatLng(locations[i][1], locations[i][2]),
    map: map
    });

    google.maps.event.addListener(marker, 'click', (function(marker, i) {
    return function() {
        infowindow.setContent(locations[i][0]);
        infowindow.open(map, marker);
    }
    })(marker, i));
}

任何帮助将不胜感激。

【问题讨论】:

    标签: javascript google-maps google-maps-api-3


    【解决方案1】:

    检查“locations.length”是否存在,如果不存在,它将返回 null 并在长度值上出错。你可以做这样的事情 -

    if (!locations ==0){
    for (var i = 0; i < locations.length; i++)
    ....
    

    您可以找到更多答案Uncaught TypeError: Cannot read property 'length' of null

    【讨论】:

    • 检查“locations.length”是否存在 - 你在上面的代码中是在哪里做的?
    【解决方案2】:

    通过向#map 添加固定高度来解决(以前是 100%)。

    【讨论】:

    • 这与您在问题中提到的错误完全无关。
    猜你喜欢
    • 2012-09-05
    • 1970-01-01
    • 2020-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-21
    • 2014-12-05
    • 1970-01-01
    相关资源
    最近更新 更多