Skip to content

Instantly share code, notes, and snippets.

@jarrettbarnett
Created November 10, 2017 16:40
Show Gist options
  • Save jarrettbarnett/5ffef26726a83b9c94977a204272bff4 to your computer and use it in GitHub Desktop.
Save jarrettbarnett/5ffef26726a83b9c94977a204272bff4 to your computer and use it in GitHub Desktop.
This problem is usually due to the map div not being rendered before the javascript runs that needs to access it. You should put your initialization code inside an onload function or at the bottom of your HTML file, just before the tag, so the DOM is completely rendered before it executes (note that the second option is more sensitive to invalid HTML). Note, as pointed out by matthewsheets this also could be cause by the div with that id not existing at all in your HTML (the pathological case of the div not being rendered) Adding code sample from wf9a5m75's post to put everything in one place: <script type="text/javascript"> function initialize() { var latlng = new google.maps.LatLng(-34.397, 150.644); var myOptions = { zoom: 8, center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP }; var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); } google.maps.event.addDomListener(window, "load", initialize); </script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment