【发布时间】:2014-11-21 16:52:02
【问题描述】:
我在使用 Google Maps 时遇到问题,我在隐藏的 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>
【问题讨论】:
-
你能用你的标记发布一个小提琴,以便我们看看它是如何呈现的吗?
标签: html css javascript mobile