我正在我的网站上使用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));
}
任何帮助将不胜感激。
检查“ locations.length”是否存在,如果不存在,则返回null,并且长度值出错。你可以做这样的事情-
if (!locations ==0){
for (var i = 0; i < locations.length; i++)
....
您可以找到更多答案Uncaught TypeError: Cannot read property 'length' of null
已通过在#map中添加固定高度来解决(以前是100%)。