I am using the Google Maps API for the webpage I'm working on. My problem right now is that the map loads exactly as I expect it, but only after the page is refreshed. If I do not refresh the page, all I see is canvas. The problem remains if I leave the page and return to it, so it seems to be a problem when calling the card in initialize.
Code:
<style>
#map_canvas {
width: 670px;
height: 400px;
background-color: #CCC;
}
</style>
<script src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script>
function initialize() {
var myLatLng = new google.maps.LatLng(31.247681, 121.449504);
var map_canvas = document.getElementById('map_canvas');
var map_options = {
center: new google.maps.LatLng(31.248195, 121.447431),
zoom: 16,
mapTypeControl: false,
panControl: false,
zoomControlOptions: {
position: google.maps.ControlPosition.LEFT_CENTER
},
streetViewControl: false,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(map_canvas, map_options);
var contentString =
'<b>Leung Gallery</b>' +
'<p>50 Moganshan Rd. Building 7, Unit 108, Shanghai</p>' +
'<p>上海普陀区莫干山路50号7号楼108室</p>';
var infowindow = new google.maps.InfoWindow({
content: contentString
});
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
});
google.maps.event.trigger(map, 'resize');
google.maps.event.addListener(marker, 'click', function(){
infowindow.open(map,marker);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
Looking through the previous questions, I did not find anything that exactly matched my problem, but I saw some similar situations that revolved around google.maps.event.addDomListener(window, 'load', initialize);, so I think this might be the problem. Any help is appreciated.
: . $(document).bind("projectLoadComplete", initialize); google.maps.event.addDomListener(window, 'load', initialize); . , . , , Cargo Collective.