Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google map error: a is null

I have a program that I want to use google maps for. The problem is I get an error that says a is null where a is a var used in the google map api. Here is how I call my google map:

//Creates a new center location for the google map
    var latlng = new google.maps.LatLng(centerLatitude, centerLongitude);

    //The options for the google map
    var myOptions = {
        zoom: 7,
        maxZoom: 12,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    //Creates the new map
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

And here is what my HTML tag looks like:

<div id = "map_canvas"></div>

I get the lat and lng on page load through the url. These values are passed in correctly so I know that is not the problem. I think that it has to do with the var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); not being correct. Any suggestions?

EDIT: Here is the error message:

a is null fromLatLngToPoint(a=null) yg(a=null, b=Object { zoom=7, maxZoom=12, more...}) d(d=Document Default.aspx?lat=30.346317&lng=105.46313, f=[function()]) d(a=undefined) d() [Break On This Error] function Qf(a){a=a.f[9];return a!=i?a:...);function sg(a){a[ic]&&a[ic]Vb}

like image 384
Soatl Avatar asked Mar 29 '11 20:03

Soatl


People also ask

Can Google map make a mistakes?

We've all likely experienced a Google Maps mistake at some point. Maybe it took you down a strange route, led you into worse traffic, or kept bringing up a totally different location than you wanted. With technology, this stuff will happen.


3 Answers

Make sure you specify the size of the element that holds the map. For example:

<div id="map_canvas" style="width: 500px; height: 500px;"></div> 

Also make sure your map variable is defined in the global scope and that you initialize the map once the DOM is loaded.

like image 107
Michal Avatar answered Sep 29 '22 19:09

Michal


You are probably not listening for the onload event that fires when the page is completely loaded. As a result, your script is running but the div you are creating doesn't yet exist. Use jQuery to listen for this event, like so:

$(document).ready(function () {
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
});

If you don't want to use jQuery, then add an event listener to body.onload

like image 41
Travis Webb Avatar answered Sep 29 '22 20:09

Travis Webb


This rather cryptic error means that the script can't find the map div. This could happen for a couple of reasons.

1. You're using the wrong ID to refer to the map.

Check your ids (or classes) and make sure the element you're referring to actually exists.

2. You're executing the script before the DOM is ready.

Here's a jQuery example. Notice we're triggering initialise on document ready, not onDOMReady. I've taken the liberty of wrapping the script in a closure.

(function($) {
  function initialize() {
    var mapOptions = {
      center: new google.maps.LatLng(-34.397, 150.644),
      zoom: 8,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map"),
        mapOptions);
  }
  $(document).ready(initialize);
})(jQuery)

You could also use:

google.maps.event.addDomListener(window, 'load', initialize);

if you prefer a Google solution.

like image 20
superluminary Avatar answered Sep 29 '22 21:09

superluminary