我在Google上遇到了问题,我正在将地图呈现在一个隐藏的DIV元素中。地图呈现,但只有左上角显示,即使在我移动地图。
我发现的解决方案是等待呈现地图和点,直到用户实际显示地图。我这样做的方法是让一个间隔计数器每半秒迭代一次,查看用户是否单击按钮来打开/显示元素。
这在桌面浏览器上有效,但它不能解决移动浏览器的相同问题。这个角落仍然被卡住了,我不知道如何从移动的角度来处理这个问题。
<script type="text/javascript">
var locations = [SOME DATA];
var map;
var bounds;
function initialize() {
var myLatlng = new google.maps.LatLng(0, 0);
var mapOptions = {
zoom: 5,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
bounds = new google.maps.LatLngBounds();
for (var i = 0; i < locations.length; i++) {
var entity = locations[i];
var myLatLng = new google.maps.LatLng(entity[6], entity[7]);
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
icon: pinBase + pin
});
if(entity[8] < 5) {
bounds.extend(marker.position);
}
var infowindow = new google.maps.InfoWindow({});
if(entity[5] == "0") {
entity[5] = "N/A";
}
contentString = "<div class=\"mapBaloon\"><b>" + entity[0] + "</b><br />WWW: <a href=\"http://" + entity[2] + "\">http://" + entity[2] + "</a><br />City: " + entity[3] + "<br />State: " + entity[4] + "<br />Phone: " + entity[5] + "<br />Distance: " + entity[8] + " miles</div>";
bindInfoWindow(marker, map, infowindow, contentString);
}
map.fitBounds(bounds);
}
function bindInfoWindow(marker, map, infowindow, html) {
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(html);
infowindow.open(map, marker);
});
}
var myVar = setInterval(function() {
google.maps.event.addDomListener(window, 'load', initialize);
if($(".commArea2").is(":visible")) {
clearInterval(myVar);
google.maps.event.trigger(map, 'resize');
map.fitBounds(bounds);
}
}, 500);
</script>发布于 2014-09-26 16:30:38
我在手机上也有类似的问题。当div被触发显示时,我调用了initialize()函数。
$('#open_btn').bind('touchstart', function(event){
initialize();
})发布于 2014-09-26 17:09:21
在我头上有几个可能的原因可能会导致这种情况。因为它在桌面上工作,而不是移动的,所以我怀疑你有种族状况。桌面更快,它将更快地加载Google。500毫秒的延迟听起来不足以在移动设备上等待。但是,你不能提高setInterval。把它处理掉。
Google提供了向您的“
https://developers.google.com/maps/documentation/javascript/examples/map-simple-async
我遇到的另一个问题是,在做任何事情之前,您确实必须确保地图上有一个显式的宽度/高度。或者设置宽度/高度属性,对所有的上/左/下/右定义“位置:绝对”,或者做其他事情。但是,如果Maps API无法计算出父元素的显式宽度,那么您将在角落中得到一个很好的框。但就像你说的,它在一个地方起作用,很可能这不是问题所在。只需确保您要在HTML或CSS中使用在移动浏览器中不支持的内容。
https://stackoverflow.com/questions/26063591
复制相似问题