I'm trying to use Google MAP API v3 with the following code.
<h2>Topology</h2>
<script src="https://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="{% url css_media 'tooltip.topology.css' %}" />
<link rel="stylesheet" type="text/css" href="{% url css_media 'tooltip.css' %}" />
<style type="text/css" >
#map_canvas {
width:300px;
height:300px;
}
</style>
<script type="text/javascript">
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);
</script>
<div id="map_canvas"> </div>
When I run this code, the browser says this.
Uncaught TypeError: Cannot read property 'offsetWidth' of null
I have no idea, since I follow the direction given in this tutorial.
Do you have any clue?
Also, make sure you're not placing hash symbol (#) inside your selector in a
statement. It's not a jQuery. Just a quick reminder for someone in a hurry.
Here the problem was the API link without the
key
param:That way works fine.
If you're creating multiple maps in a loop, if a single map DOM element doesn't exist, it breaks all of them. First, check to make sure the DOM element exists before creating a new Map object.
Also take care not to write
correct:
If you happen to be using asp.net Webforms and are registering the script in the code behind of a page using a master page, don't forget to use the clientID of the map element (vb.net):
This is an edit of a previously deleted post to contain the content of the linked answer, in the answer itself. The purpose for re-publishing this answer is to function as a PSA for React 0.9+ users who have also stumbled into this issue.
original edited post
Also got this error while using ReactJS while following this blog post. sahat's comment here helped - see the content of sahat's comment below.